Edit: Fast du använder visst inte interrupt

Men din kod läser väl av UDR en massa gånger, oavsett om det kommit in ny data eller inte?
Nolla RXC kanske?
Så UDR måste läsas för att RXC ska nollas. Frågan är då ändå om en return-sats räcker för att UDR ska ses som läst?The Receive Complete (RXCn) Flag indicates if there are unread data present in the receive buf-
fer. This flag is one when unread data exist in the receive buffer, and zero when the receive
buffer is empty (i.e., does not contain any unread data). If the Receiver is disabled (RXENn = 0),
the receive buffer will be flushed and consequently the RXCn bit will become zero.
When the Receive Complete Interrupt Enable (RXCIEn) in UCSRnB is set, the USART Receive
Complete interrupt will be executed as long as the RXCn Flag is set (provided that global inter-
rupts are enabled). When interrupt-driven data reception is used, the receive complete routine
must read the received data from UDRn in order to clear the RXCn Flag, otherwise a new inter-
rupt will occur once the interrupt routine terminates.
Kod: Markera allt
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <avr/iom16.h>
#include <inttypes.h>
void USARTInit(uint16_t ubrr_value)
{
//Set Baud rate
UBRRH=0x00;
UBRRL=0x33;
/*Set Frame Format
>> Asynchronous mode
>> No Parity
>> 1 StopBit
>> char size 8
*/
UCSRC=(1<<URSEL)|(3<<UCSZ0);
//Enable The receiver and transmitter
UCSRB=(1<<RXEN)|(1<<TXEN);
}
void InitPWM()
{
/*
TCCR0 - Timer Counter Control Register (TIMER0)
-----------------------------------------------
BITS DESCRIPTION
NO: NAME DESCRIPTION
--------------------------
BIT 7 : FOC0 Force Output Compare [Not used in this example]
BIT 6 : WGM00 Wave form generartion mode [SET to 1]
BIT 5 : COM01 Compare Output Mode [SET to 1]
BIT 4 : COM00 Compare Output Mode [SET to 0]
BIT 3 : WGM01 Wave form generation mode [SET to 1]
BIT 2 : CS02 Clock Select [SET to 0]
BIT 1 : CS01 Clock Select [SET to 0]
BIT 0 : CS00 Clock Select [SET to 1]
The above settings are for
--------------------------
Timer Clock = CPU Clock (No Prescalling)
Mode = Fast PWM
PWM Output = Non Inverted
*/
TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);
TCCR2|=(1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);
//Set OC0 PIN as output. It is PB3 on ATmega16
//Set OC2 PIN as output. It is PD7 on ATmega16
DDRB|=(1<<PB3);
DDRD|=(7<<PB7);
}
/******************************************************************
Sets the duty cycle of output.
Arguments
---------
duty: Between 0 - 255
0= 0%
255= 100%
The Function sets the duty cycle of pwm output generated on OC0 PIN
The average voltage on this output pin will be
duty
Vout= ------ x 5v
255
This can be used to control the brightness of LED or Speed of Motor.
*********************************************************************/
// Ställer in alla kanaler
void PWMOutputCH1(uint8_t duty)
{
OCR0=duty;
}
void PWMOutputCH4(uint8_t duty)
{
OCR2=duty;
}
/********************************************************************
Simple Wait Loop
*********************************************************************/
void Wait()
{
_delay_loop_2(10000);
}
void USARTWriteChar(char data)
{
//Wait untill the transmitter is ready
while(!(UCSRA & (1<<UDRE)))
{
//Do nothing
}
//Now write the data to USART buffer
UDR=data;
}
char USARTReadChar()
{
//Wait untill a data is available
while(!(UCSRA & (1<<RXC)))
{
//Do nothing
}
//Now USART has got data from host
//and is available is buffer
return UDR;
}
void main() {
USARTInit(51);
InitPWM();
int FanCH='a';
char data;
char channel='a';
int outval='0';
//Do this forever
while(1)
{
//Läser vilken kanal det handlar om. a,b,c eller d (och värde)
FanCH = USARTReadChar();
USARTWriteChar(FanCH);
// 97 = 'a'
if (FanCH=='a')
{
//Gå till kanal 1
channel='a';
}
// 98 = 'b'
else if (FanCH=='b')
{
//Gå till kanal 2
channel='b';
}
// 99 = 'c'
else if (FanCH=='c')
{
//Gå till kanal 3
channel='c';
}
// 100 = 'd'
else if (FanCH=='d')
{
//Gå till kanal 4
channel='d';
}
//CH1 Läser com port, skickar ut värde till OCR0
data = USARTReadChar();
if (data==48)
{
outval=0;
}
else if (data==49)
{
outval=30;
}
else if (data==50)
{
outval=90;
}
else if (data==51)
{
outval=120;
}
else if (data==52)
{
outval=190;
}
else if (data==53)
{
outval=210;
}
else if (data==54)
{
outval=225;
}
else if (data==55)
{
outval=235;
}
else if (data==56)
{
outval=245;
}
else if (data==57)
{
outval=255;
}
switch (channel)
{
case 'a':
PWMOutputCH1(outval);
_delay_ms(100);
break;
case 'd':
PWMOutputCH4(outval);
_delay_ms(100);
break;
}
}
}
Kod: Markera allt
while(1)
{
//Läser vilken kanal det handlar om. a,b,c eller d (och värde)
FanCH = USARTReadChar();
USARTWriteChar(FanCH);
Kod: Markera allt
void sendChar(unsigned char data)
{
while(!(UCSR0A&(1<<UDRE0)));
UDR0 = data;
}
unsigned char getChar()
{
while(!(UCSR0A&(1<<RXC0)));
return UDR0;
}