void uartswSetBaudRate(u32 baudrate) { // set timer prescaler timer1SetPrescaler(TIMER_CLK_DIV1); // calculate division factor for requested baud rate, and set it UartswBaudRateDiv = (u16)((F_CPU+(baudrate/2L))/(baudrate*1L)); }
//Main für LCD-Test int main(void) { //u08 a=0; // initialize our libraries // initialize the UART (serial port) // uartInit(); // make all rprintf statements use uart for output // rprintfInit(uartSendByte); // turn on and initialize A/D converter //a2dInit(); // initialize the timer system timerInit(); // print a little intro message so we know things are working // rprintf("\r\nWelcome to AVRlib!\r\n"); timer1SetPrescaler(4); timerAttach(TIMER1OVERFLOW_INT, lcdPrepare); // initialize LCD //lcdInit(); // direct printf output to LCD // rprintfInit(lcdDataWrite); // print message on LCD //rprintf("Welcome to AVRlib!"); //DDRC = 0x00; //PORTC = 0x00; // display a bargraph of the analog voltages on a2d channels 0,1 while(1) { // lcdGotoXY(0,0); // lcdProgressBar(a2dConvert8bit(0), 255, 20); // rprintf(" X: %d", a2dConvert8bit(0)); // rprintf(" Sample: %d", a++); // lcdGotoXY(0,1); // lcdProgressBar(a2dConvert8bit(1), 255, 20); // rprintf(" Y: %d", a2dConvert8bit(1)); if(lcdPrepared){ lcdClear(); lcdGotoXY(0,0); rprintf("0. Zeile"); lcdGotoXY(1,1); rprintf("1. Zeile %d",'ü'); lcdGotoXY(2,2); rprintf("2. Zeile"); lcdGotoXY(3,3); rprintf("3. Zeile"); } } return 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 }
//! initializes software PWM system void servoInit(void) { u08 channel; // disble the timer1 output compare A interrupt cbi(TIMSK, OCIE1A); // set the prescaler for timer1 timer1SetPrescaler(TIMER_CLK_DIV256); // attach the software PWM service routine to timer1 output compare A timerAttach(TIMER1OUTCOMPAREA_INT, servoService); // enable and clear channels for(channel=0; channel<SERVO_NUM_CHANNELS; channel++) { // set minimum position as default ServoChannels[channel].duty = SERVO_MIN; // set default port and pins assignments ServoChannels[channel].port = _SFR_IO_ADDR(SERVO_DEFAULT_PORT); //ServoChannels[channel].port = (unsigned char)&SERVO_DEFAULT_PORT; ServoChannels[channel].pin = (1<<channel); // set channel pin to output // THIS IS OBSOLETED BY THE DYNAMIC CHANNEL TO PORT,PIN ASSIGNMENTS //outb(SERVODDR, inb(SERVODDR) | (1<<channel)); } // set PosTics ServoPosTics = 0; // set PeriodTics ServoPeriodTics = SERVO_MAX*9; // set initial interrupt time u16 OCValue; // read in current value of output compare register OCR1A OCValue = inb(OCR1AL); // read low byte of OCR1A OCValue += inb(OCR1AH)<<8; // read high byte of OCR1A // increment OCR1A value by nextTics OCValue += ServoPeriodTics; // set future output compare time to this new value outb(OCR1AH, (OCValue>>8)); // write high byte outb(OCR1AL, (OCValue & 0x00FF)); // write low byte // enable the timer1 output compare A interrupt sbi(TIMSK, OCIE1A); }
void init(void) { //Make outputs low cbi(PWM_PORT, PWM_BIT); cbi(SD_PORT, PWM_BIT); //Make outputs outputs sbi(PWM_DDR, PWM_BIT); //Pull-up swithces sbi(ESTOP_PORT, ESTOP_BIT); sbi(BRAKE_PORT, BRAKE_BIT); //RGB Outputs sbi(RGB_DDR, RED); sbi(RGB_DDR, GREEN); sbi(RGB_DDR, BLUE); //RGB Off sbi(RGB_PORT, RED); sbi(RGB_PORT, GREEN); sbi(RGB_PORT, BLUE); //Initialise the ADC a2dInit(); //Initialise timer 1 timer1Init(); timer1SetPrescaler(TIMER_CLK_DIV1); timer1PWMInitICR(TOP_COUNT); //Enable PWM with top count timer1PWMAOn(); //Turn PWM on rgb(green); sei(); //Enable interupts }