/* main.c - delay loop example for Dragon12-Plus
 * Copyright 2008 Colin D. Bennett <colin@gibibit.com>.
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program uses the delay_ms() function to time the LEDs so they 
 * cycle in real time.  They complete a full rotation once per second.
 * (8 LEDs at 125 ms each = 1 second per rotation.)
 *
 * The delay_ms() function uses an assembly language delay loop to
 * implement the delay.  You should call delay_ms() with a constant value
 * so that the floating point arithmetic operations are done at
 * compile time, not at run time.
 */

#include "hc12/hcs12.h"
#include "hc12/delay.h"

int main(void)
{
	/*** Configure LEDs. ***/
	DDRB = 0xFF;   // Set port B to output for LEDs (PB7:0 are LED anodes).
	DDRJ = 0xFF;   // Set port J to output, so we can enable LEDs w/ PJ1.
	PTJ = 0;       // Pull PJ1 low to enable LEDs.
	DDRP = 0xFF;   // Set port P to output, (to turn off 7-seg display).
	PTP = 0xFF;    // PP3:0 are 7-segment common cathodes: turn them off.

	PORTB = 1;     // Initially light up PB0 LED.
	/* Every time the timer overflow ISR is called, 
	 * it will rotate the PORTB value to the left. */


	// Infinite loop -- the real work is in the timer_ovf_isr.
	while (1)
	{
		uint8_t x = PORTB;
		PORTB = (x << 1) | ((x & 0x80) >> 7);  // rotate PORTB value left
		delay_ms(125);
	}
}

