Beispiel #1
0
void uartswSetBaudRate(u32 baudrate)
{
	u16 div;

	// set timer prescaler
	if( baudrate > (F_CPU/64L*256L) ) 
	//CHANGE THE ABOVE LINE IF CLOCK FREQ OF AVRLINX BOARD DIFFERS
	//FROM AVRSAT
	{
		// if the requested baud rate is high,
		// set timer prescalers to div-by-64
		timer2SetPrescaler(TIMERRTC_CLK_DIV64);
		timer0SetPrescaler(TIMER_CLK_DIV64);
		div = 64;
	}
	else
	{
		// if the requested baud rate is low,
		// set timer prescalers to div-by-256
		timer2SetPrescaler(TIMERRTC_CLK_DIV256);
		timer0SetPrescaler(TIMER_CLK_DIV256);
		div = 256;
	}

	// calculate division factor for requested baud rate, and set it
	//UartswBaudRateDiv = (u08)(((F_CPU/64L)+(baudrate/2L))/(baudrate*1L));
	//UartswBaudRateDiv = (u08)(((F_CPU/256L)+(baudrate/2L))/(baudrate*1L));
	UartswBaudRateDiv = (u08)(((F_CPU/div)+(baudrate/2L))/(baudrate*1L));
}
Beispiel #2
0
void uartswSetBaudRate(uint32_t baudrate)
{
	uint16_t div;

	// set timer prescaler
	if ( baudrate > (F_CPU/64L*256L) )
	{
		// if the requested baud rate is high,
		// set timer prescalers to div-by-64
		timer0SetPrescaler(TIMER_CLK_DIV64);
		div = 64;
	}
	else
	{
		// if the requested baud rate is low,
		// set timer prescalers to div-by-256
		timer0SetPrescaler(TIMER_CLK_DIV256);
		div = 256;
	}

	// calculate division factor for requested baud rate, and set it
	//UartswBaudRateDiv = (uint8_t)(((F_CPU/64L)+(baudrate/2L))/(baudrate*1L));
	//UartswBaudRateDiv = (uint8_t)(((F_CPU/256L)+(baudrate/2L))/(baudrate*1L));
	UartswBaudRateDiv = (uint8_t)(((F_CPU/div)+(baudrate/2L))/(baudrate*1L));
}
Beispiel #3
0
void timer0Init(uint8_t prescale)
{
	// initialize timer 0
	timer0SetPrescaler(prescale);			// set prescaler
	TCNT0 = 0;								// reset TCNT0

	timer0ClearOverflowCount();				// initialize time registers
}
Beispiel #4
0
void init_motors()
{
		//off
	cbi(PORTD, 4); //motor off
	cbi(PORTD, 5); //motor off
	cbi(PORTD, 7); //motor off
	cbi(PORTB, 3); //motor off

	sbi(DDRD, 4); //motor outs
	sbi(DDRD, 5); //motor outs
	sbi(DDRD, 7); //motor outs
	sbi(DDRB, 3); //motor outs


	timerInit();
	cbi(TIMSK, TOIE0); // disable timer 0 overflow interrupt
	cbi(TIMSK, TOIE1); // disable timer 1 overflow interrupt
	cbi(TIMSK, TOIE2); // disable timer 2 overflow interrupt

	timer0SetPrescaler(TIMER_CLK_DIV1);
	timer1SetPrescaler(TIMER_CLK_DIV1);
	timer2SetPrescaler(TIMER_CLK_DIV1);

	// setup PWM timer 0
	OCR0 = 0;	// duty cycle 0%
	// enable timer0 as PWM phase correct, todo: use fast pwm
	sbi(TCCR0,WGM00);
	cbi(TCCR0,WGM01);
	// turn on channel (OC0) PWM output
	// set OC0 as non-inverted PWM
	cbi(TCCR0,COM00);
	sbi(TCCR0,COM01);


	// setup timer 1A/B
	timer1PWMInit(8);	// pwm 8 bit

	timer1PWMASet(0);	// duty cycle 0%
	timer1PWMBSet(0);	// duty cycle 0%
	timer1PWMAOn();
	timer1PWMBOn();

	//setup PWM timer 2
	OCR2 = 0;	// duty cycle 0%
	// enable timer2 as PWM phase correct, todo: use fast pwm
	sbi(TCCR2,WGM20);
	cbi(TCCR2,WGM21);
	// turn on channel (OC0) PWM output
	// set OC0 as non-inverted PWM
	cbi(TCCR2,COM20);
	sbi(TCCR2,COM21);


	// enable timer interrupt
	sbi(TIMSK, TOIE0); //enable timer 0 overflow interrupt
}
Beispiel #5
0
void rtcInit(void)
{
	// set up timer for RTC operation
	// initialize real-time registers
	RtcTime.totaltics = 0;
	RtcTime.tics = 0;
	RtcTime.seconds = 0;
	RtcTime.minutes = 0;
	RtcTime.hours = 0;
	RtcTime.day = 1;
	RtcTime.month = 1;
	RtcTime.year = 2000;

	// select the correct RTC timer based on bit defines
	#ifdef AS2
		// use timer2 for most AVRs
		// initialize timer 2
		timer2Init();
		// count with 32.768KHz/8
		timer2SetPrescaler(TIMER_CLK_DIV8);
		// switch to asynchronous input (32KHz crystal)
		sbi(ASSR, AS2);
		// attach service to real-time clock interrupt
		// rtcService() will be called at ((32768/8)/256) = 16Hz
		timerAttach(TIMER2OVERFLOW_INT, rtcService);
	#else
	#ifdef AS0
		// use timer0 for ATmega103, ATmega128
		// initialize timer 0
		timer0Init();
		// count with 32.768KHz/8
		timer0SetPrescaler(TIMER_CLK_DIV8);
		// switch to asynchronous input (32KHz crystal)
		sbi(ASSR, AS0);
		// attach service to real-time clock interrupt
		// rtcService() will be called at ((32768/8)/256) = 16Hz
		timerAttach(TIMER0OVERFLOW_INT, rtcService);
	#endif
	#endif
}
Beispiel #6
0
static void avr_init(void)
// Initializes AVR controller
{
	MAX6675S_DDR |= BV(MAX6675_CSPIN);		// setup direction for CS pin
	DISABLE_6675;							// Set CS = high for MAX6675	
	initZerocross();						// init ext Int0
	initTriac();							// init Triac pin & direction


#ifdef REG_PID
//	initPID(&PID_data, 1, 0, 10, 10, -10);

//void pid_Init(int16_t p_factor, int16_t i_factor, int16_t d_factor, struct PID_DATA *pid);
	pid_Init(P_FACTOR, I_FACTOR, D_FACTOR, &PID_data);
#endif

#ifdef REG_PD
	initPD(&PD_data, 800, 1000);
#endif


	//attach functions to timer interrupts
	timerAttach(TIMER0OVERFLOW_INT, sample);
	timerAttach(TIMER2OUTCOMPAREA_INT, stopTriac);
	timerAttach(TIMER1OUTCOMPAREA_INT, fireTriac);

	//enable output compare for timers
	sbi(TIMSK0, TOIE0);
	sbi(TIMSK1, OCIE1A);
	sbi(TIMSK2, OCIE2A);

	//set OCR values. 
//	OCR0A = PHASE_ANGLE_LIMIT_HIGH;				//firing angle of triac
	OCR2A = 1;									//length of firing pulse
	OCR1A = PHASE_ANGLE_LIMIT_HIGH;				//temperature sample frequency

	timer0SetPrescaler(TIMER_CLK_DIV1024);		//start temp. sampling

/*************************************** 
  * Timer0 runs at 24Mhz/1024 = 
  *
  * 
  *
  ***************************************/

#ifndef DEBUG_SIM
	#ifdef DEBUG_SER
	// initialize the UART (serial port)
	uartInit();

	// set the baud rate of the UART for our debug/reporting output
	uartSetBaudRate(57600);

	// make all rprintf statements use uart for output
	rprintfInit(uartSendByte);

	// print a little intro message so we know things are working
	rprintf("Reflow!\r\n");

//	vt100SetCursorMode(1);
//	vt100SetAttr(VT100_BLINK_OFF);
//	vt100SetAttr(VT100_USCORE_OFF);
//	vt100SetAttr(VT100_REVERSE);

	//vt100ClearScreen();
	// start command line
//	goCmdline();
 
	#endif

	// initialize SPI interface
	spiInit();
#endif

//	SET_HALF_PHASE;
//	SET_SKIP_PHASE;
//	skips = 15;
	//enable interrupts
	sei();


    return;
}