Ich habe mal schnell was zusammengeschrieben. Die Timerwerte und OCRx-Werte mussst du für dich anpassen. Es geht mit den Zahlen am Tastenblock 2,4,6,8,0.
Code:
#include <avr/io.h>
#include <stdbool.h>
#include <stdlib.h>
#include <avr/io.h>
#include <AVR/iom8.h>
#include <inttypes.h>
#include <avr/interrupt.h>
#define F_CPU 8000000 // clock
#define BAUD 9600
#define bauddivider (unsigned int)(F_CPU / BAUD / 16 - 0.5)
volatile unsigned char rxwert=0x30;
ISR(USART_RXC_vect){
rxwert= UDR;
PORTB=(1<<PB0);
}
void timer1_init(void)
{
TCCR1A = (1<<WGM11)|(1<<COM1A1)|(1<<COM1A0)|(1<<COM1B1)|(1<<COM1B0); // initalize mega8 Timer1 with 9Bit, inverted, Prescaler 256
TCCR1B = (1<<CS12); // this gives us a signal with 21.76ms at 12MHz
OCR1A = 486; // pulse of 1.5ms 512- 1500*(F_CPU/256/1000000)/2
OCR1B = 486;
}
void usart_init(void){
UBRRL = bauddivider; //set baud rate
UBRRH = bauddivider >> 8;
UCSRB = (1<<RXCIE)|(1<<RXEN);
UCSRC = (1<<URSEL)|(3<<UCSZ0);
}
int main (void)
{
timer1_init();
usart_init();
DDRB = (1<<PB0)|(1<<PB1)|(1<<PB2); /* Pin PB1 2 als Ausgang für Servo */
DDRD &= (~ (1<<PD2)|(1<<PD3)|(1<<PD0)|(1<<PD1)); /* Pin D als Eingang */
PORTD |= (1<<PD2)|(1<<PD3)|(1<<PD0)|(1<<PD1); /* Pull Up aktivieren */
sei();
while(1){
if(rxwert==0x30){
if (!( PIND & (1<<PIND2))) /* mache was wenn PinD2 low ist */
{
OCR1A=470;
}
if (!( PIND & (1<<PIND3))) /* mache was wenn PinD3 low ist */
{
OCR1A=499;
}
if (( PIND & (1<<PIND2))&&(PIND & (1<<PIND3))) { /* Mitte wenn keine Taste */
OCR1A = 486;
}
if (!( PIND & (1<<PIND0))) /* mache was wenn PinD0 low ist */
{
OCR1B=470;
}
if (!( PIND & (1<<PIND1))) /* mache was wenn PinD1 low ist */
{
OCR1B=499;
}
if (( PIND & (1<<PIND0))&&(PIND & (1<<PIND1))) { /* Mitte wenn keine Taste */
OCR1B=486;
}
}
switch(rxwert){
case 0x38 :
OCR1A=470;
break;
case 0x32 :
OCR1A=499;
break;
case 0x34 :
OCR1B=470;
break;
case 0x36 :
OCR1B=499;
break;
}
}
}
Ein gutes C-Tutorial ist auch das hier: http://www.mikrocontroller.net/artic...R-GCC-Tutorial
Hubert
Lesezeichen