Hi,
Ich habe folgende Probleme beim Ansteuern einer Servo:
1.) Wenn ich die Funktion my_delay_us(y) verwende dreht sich die servo
ruckartig im GUZ bis sie anstellt und nicht passiert mehr, bei
_delay_us("zahl") klappt alles wunderbar.
2.) Wenn ich über UART einen Wert schicken möchte, dann löst es mir den
Interrupt nonstop aus, obwohl ich das RXE in UCSRA wieder auf 0 stelle.
1 | #include <avr/io.h>
|
2 | #include <avr/delay.h>
|
3 | #include <avr/interrupt.h>
|
4 |
|
5 | #define F_CPU 1000000UL
|
6 | #define BAUD 4800UL
|
7 | #define MYUBRR ((F_CPU+BAUD*8)/(BAUD*16)-1)
|
8 |
|
9 | #define USART_RXC_vect _VECTOR(11)
|
10 |
|
11 | void init();
|
12 | void USART_Init( unsigned int ubrr);
|
13 | void USART_Transmit(unsigned char data);
|
14 | void my_delay_us(uint16_t count);
|
15 |
|
16 | uint8_t x = 150;
|
17 | uint16_t y = 150;
|
18 |
|
19 | ISR(USART_RXC_vect) {
|
20 | if(50<UDR && UDR<240)
|
21 | x =UDR;
|
22 | y = 10*x;
|
23 | UCSRA &= ~(0b10000000); //Interrupt abschalten
|
24 |
|
25 | }
|
26 |
|
27 | int main(void)
|
28 | {
|
29 | init();
|
30 | while(1)
|
31 | {
|
32 | for (uint8_t i = 0; i<50;i++)
|
33 | {
|
34 | PORTB = 0x01;
|
35 | _delay_us(1000); //delay should depends on y with my_delay_us(y)
|
36 | PORTB = 0x00;
|
37 | _delay_us(19000);
|
38 | }
|
39 |
|
40 |
|
41 | for (uint8_t i = 0; i<50;i++)
|
42 | {
|
43 | PORTB = 0x01;
|
44 | _delay_us(2000);
|
45 | PORTB = 0x00;
|
46 | _delay_us(18000);
|
47 | }
|
48 | }
|
49 | }
|
50 |
|
51 | void init() {
|
52 | DDRB = 0x01;
|
53 | PORTB = 0x00;
|
54 | USART_Init(MYUBRR);
|
55 |
|
56 | }
|
57 |
|
58 | void USART_Init( unsigned int ubrr)
|
59 | {
|
60 |
|
61 | UBRRH = MYUBRR >> 8;
|
62 | UBRRL = MYUBRR & 0xFF;
|
63 |
|
64 | UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE);
|
65 |
|
66 | UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
|
67 |
|
68 | sei();
|
69 | }
|
70 |
|
71 | void my_delay_us(uint16_t count) {
|
72 | while(count--) {
|
73 | _delay_us(1);
|
74 | }
|
75 | }
|
76 |
|
77 | void USART_Transmit( unsigned char data )
|
78 | {
|
79 |
|
80 | while ( !( UCSRA & (1<<UDRE)) );
|
81 | UDR = data;
|
82 | }
|
Jemand eine Idee an was sowas liegen kann?
Gruss Bert