AVR ATmega328p and MCP3201 issue - c

I have a problem with SPI Interface in ATmega328p. I wrote a code that can SPI communicate with MCP3201(analog to digital converter), but I received the wrong value from MCP3201. The value should be between 600 - 700, but I get 2.
I use MCP9700(temperature sensor) to received voltage value and convert with ADC with MCP3201. I read the MCP3201' data sheet. It needs to remove trash bit. I wrote the code to show you below.
Can you check my code and schematic?
main.c
#define F_CPU 8000000L
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
#define CS PB2
#define CS_DDR DDB2
#define MOSI DDB3
#define CLK DDB5
void USART_Init(unsigned int ubrr) {
UBRR0 = ubrr;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00);
}
void USART_Transmit( unsigned char data ) {
while ( !( UCSR0A & (1 << UDRE0)) );
UDR0 = data;
}
void print(unsigned char *buffer) {
for(int i=0; buffer[i] != 0; i++){
USART_Transmit(buffer[i]);
}
}
void SPI_Init()
{
/* set MOSI CLK CS as Output*/
DDRB |= (1 << CS_DDR) | (1 << CLK) | (1 << MOSI);
// Chip select high
PORTB |= (1 << CS);
// Chip select low
PORTB &= ~(1 << CS);
/* Enable SPI, Master mode, clk/16 */
SPCR |= (1 << SPE) | (1 << MSTR) | (1 << SPR0);
}
uint16_t SPI_READ()
{
uint8_t rx_byte;
uint16_t rx_12bits;
PORTB &= ~(1 << CS); // Chip select low
SPDR = 0xFF; // put dummy byte in SPDR
while(!(SPSR & (1<<SPIF))); // wait for SPIF high
rx_byte = SPDR & 0b00111111; // copy SPDR out
rx_12bits = rx_byte << 7;
SPDR = 0xFF; // put dummy byte in SPDR
while(!(SPSR & (1<<SPIF))); // wait for SPIF high
rx_byte = SPDR >>= 1; // copy SPDR out
rx_12bits |= rx_byte; // Concat bit
PORTB |= (1 << CS); // Chip select high
return rx_12bits;
}
int main(void) {
USART_Init(53);
SPI_Init();
uint16_t sensor;
// uint16_t temp;
unsigned char text[] = "Temperature = ";
unsigned char buffer[10];
while (1) {
sensor = SPI_READ(); // Read data from sensor
// temp = (((sensor/4096.0) * 5) - 0.5) * 100 ;
sprintf(buffer,"%u",sensor ); // convert to string test with raw data
strcat(buffer, " °C\n");
print(text);
print(buffer);
_delay_ms(1000);
}
}
schematic
EDIT 1:
#define F_CPU 8000000L
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define CS PB2
#define CS_DDR DDB2
#define MOSI DDB3
#define CLK DDB5
void USART_Init(unsigned int ubrr) {
UBRR0 = ubrr;
UCSR0B |= (1 << RXEN0) | (1 << TXEN0);
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00);
}
void USART_Transmit( unsigned char data ) {
while ( !( UCSR0A & (1 << UDRE0)) );
UDR0 = data;
}
void print(unsigned char *buffer) {
for(int i=0; buffer[i] != 0; i++){
USART_Transmit(buffer[i]);
}
}
void SPI_Init()
{
/* set MOSI CLK CS as Output*/
DDRB |= (1 << CS_DDR) | (1 << CLK) | (1 << MOSI);
// Chip select high
PORTB |= (1 << CS);
// Chip select low
PORTB &= ~(1 << CS);
/* Enable SPI, Master mode, clk/16 */
SPCR |= (1 << SPE) | (1 << MSTR) | (1 << SPR0);
}
uint16_t SPI_READ()
{
uint16_t high_byte;
uint16_t low_byte;
uint16_t out_12bits;
PORTB &= ~(1 << CS); // Chip select low
SPDR = 0xFF; // put dummy byte in SPDR
while(!(SPSR & (1<<SPIF))); // wait for SPIF high
/*xx0[B11][B10][B9][B8][B7]*/
high_byte = SPDR; // copy SPDR out
SPDR = 0xFF; // put dummy byte in SPDR
while(!(SPSR & (1<<SPIF))); // wait for SPIF high
/*[B6][B5][B4][B3][B2][B1][B0][B1]*/
low_byte = SPDR; // copy SPDR out
/*xx0[B11][B10][B9][B8][B7] 0 0 0 0 0 0 0 0 */
/* OR */
/*000 0 0 0 0 0 [B6][B5][B4][B3][B2][B1][B0][B1]*/
/*---------------------------------------------------------*/
/*xx0[B11][B10][B9][B8][B7][B6][B5][B4][B3][B2][B1][B0][B1]*/
out_12bits = (high_byte << 8) | low_byte; // Concatenate bit
/*[B11][B10][B9][B8][B7][B6][B5][B4][B3][B2][B1][B0][B1]000*/
out_12bits <<= 3; // Shift left 3
/*0000[B11][B10][B9][B8][B7][B6][B5][B4][B3][B2][B1][B0]*/
out_12bits >>= 4; // Shift right 4
PORTB |= (1 << CS); // Chip select high
return out_12bits;
}
int main(void) {
USART_Init(53); // SPI intial
SPI_Init(); // USART initial
uint16_t sensor;
float temp;
unsigned char text[] = "Temperature = ";
unsigned char buffer[10];
while (1) {
sensor = SPI_READ(); // Read data from sensor
temp = (((sensor/4096.0) * 5.0) - 0.5) * 100.0 ; // Convert Analog value to temperature
dtostrf(temp, 3, 2, buffer); // Convert Float to string
strcat(buffer, " °C\n"); // Concatenate unit
print(text); // Print First Text
print(buffer); // Print temperature and unit
_delay_ms(1000);
}
}

Actually i do not have the chance to test the code, but maybe this helps:
#define F_CPU 8000000L
#define BAUD 9600UL // Used within setbaud.h
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h>
#define CS PB2
#define MOSI PB3
#define MISO PB4
#define CLK PB5
void uart_init() {
#if USE_2X // Defined within setbaud.h
UCSRA |= (1<<U2X); // Setup 8 samples/bit
#else
UCSRA &= ~(1<<U2X); // Setup 16 samples/bit
#endif
UBRR0 = UBRRH_VALUE; // Defined within setbaud.h
UCSR0B |= (1<<RXEN0) | (1<<TXEN0);
UCSR0C |= (1<<UCSZ01) | (1<<UCSZ00);
}
void uart_transmit(unsigned char data ) {
while (!(UCSR0A & (1<<UDRE0)));
UDR0 = data;
}
void print(unsigned char *buffer) {
for(unsigned int i=0; buffer[i] != 0; i++){
uart_transmit(buffer[i]);
}
}
void spi_init()
{
// Set MISO and Chip Select as Input
DDRB &= ~((1<<CS) | (1<<MISO));
// Set pullup resistors for MISO and Chip Select
PORTB |= (1<<CS) | (1<<MISO);
// SPI
// - Mode: Master
// - Prescaler: 16
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0);
// Setup SCK, MOSI and SS as output
// PORT configuration gets overwritten from SPI controller
DDRB &= (1<<CLK) | (1<<MOSI) | (1<<CS);
}
void spi_select()
{
DDRB &= ~(1<<CS);
}
void spi_deselect()
{
DDRB |= (1<<CS);
}
unsigned char spi_read()
{
SPDR = 0xFF; // Write data into the SPI Data Register and initiate a transmission
// Wait until transmission is Complete
while(!(SPSR & (1<<SPIF)))
asm volatile("NOP");
return SPDR
}
unsigned int mcp3201_data()
{
spi_select();
unsigned char high_data = spi_read();
unsigned char low_data = spi_read();
spi_deselect();
// +-------------------------------+-------------------------------+
// | HIGH | LOW |
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
// | - | - | - | - | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? |
// +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
//
// Value between 0 to (2^12 - 1) = 0 to 4095
return (((high_data & 0x0F)<<8) | low_data);
}
int main(void) {
uart_init();
spi_init();
const unsigned char text[] = "Temperature = ";
unsigned char buffer[100];
while (1)
{
unsigned int mv; = ((mcp3201_data * 5000)>>12); // Analog Voltage in mV
double temp = mv; // Calculate your temperature
dtostrf(temp, 3, 2, buffer); // Convert Float to string
strcat(buffer, " °C\n"); // Concatenate unit
print(text); // Print First Text
print(buffer); // Print temperature and unit
_delay_ms(1000);
}
}
Indeed i have not calculated the temperature. Thats your turn...

Related

Creating a 10 second delay using ISR in TIMER1 of atmega328p

I'm trying to create a 10 second delay using TIMER1(16 bit) in atmega328p, I don't know if the delay has been created or not because it takes longer duration than 10 seconds and expected output( which is to create pwm waves) is not obtained. Here I have created a 1second delay and looped it 10 times, TIMER0 is used for creating pwm waves.
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "interrupt.h"
#define SET_BIT(PORT,BIT) PORT |= (1<<BIT)
#define CLR_BIT(PORT,BIT) PORT &= ~(1<<BIT)
struct {
volatile unsigned int BIT: 1;
}
FLAG_TIMER;
void timer_configuration() //16 bit timer
{
TCCR1A = 0x00; // Normal mode of operation
TCNT1 = 0xC2F8;
TCCR1B |= ((1 << CS10) | (1 << CS12));
TCCR1B &= ~(1 << CS11); //101
sei(); // Global interrupt
}
void timer_on()
{
TIMSK1 |= (1 << TOIE1);
}
void pwm_configuration() //TIMER0 - 8 bit
{
TCCR0A |= ((1<<WGM00) | (1<<WGM01)); //setting it to fast PWM mode
TCCR0A |= (1<<COM0A1);
TCCR0A &= ~(1<<COM0A0);
TCNT0 = 0x00;
TCCR0B |= ((1<<CS00) | (1<<CS02)); //Prescaler setting 1024
TCCR0B &= ~(1<<CS01);
sei();
}
ISR(TIMER1_OVF_vect)
{
static unsigned int counter;
counter++;
if(counter >= 10)
{
FLAG_TIMER.BIT=1;
counter = 0;
TCNT1 = 0xC2F8;
TIMSK &= ~(1<< TOIE1);
}
else
{
FLAG_TIMER.BIT=0;
}
}
int main(void)
{
SET_BIT(DDRD,PD6); //CRO
timer_configuration();
pwm_configuration();
while(1)
{
timer_on();
if(FLAG_TIMER.BIT == 1)
{
OCR0A = 128; //50% dutycycle
}
}
You set the counter to 49912 on initialisation and increment your count when it overflows, but it will then start from 0, so if 15624 counts = 1 second, then your counter will increment to 10 after 15624 + 9 x 216 counts or about 38.75 seconds.
Move the TCNT1 = 0xC2F8; line in the ISR:
ISR(TIMER1_OVF_vect)
{
static unsigned int counter;
TCNT1 = 0xC2F8;
counter++;
if(counter >= 10)
{
FLAG_TIMER.BIT=1;
counter = 0;
TIMSK &= ~(1<< TOIE1);
}
else
{
FLAG_TIMER.BIT=0;
}
}
I am not familiar with ATmega, but I cannot believe that is an appropriate way to use the timer. Normally you'd up-count to a compare value with auto reset to zero, or down-count from an auto reload value to zero and let the hardware reload the counter.

ISR(TIMERn_OVF_vect) interrupt not work Atmega 2560

i have a problem whith ISR() - this function is not work. I use Atmel Studio 7, USBASP programmer, AVRDude_Prog 3.3 and RobotDyn board with Atmega 2560(Robotdyn Mega +WiFi R3 ATmega2560+ESP8266).
I want read signal from HC-SR04 sensor for mesurment distance. I read datasheet about Atmega 2560 and settings of my timers counters: start from 0 counter up to 4th counter.I tried set up each one setting of interrupts and it's not help me(
I use LEDs on PORTA for find problem with counters, interrupts and settings. But now i don't have any result.
And this simple code for enable LED, when ISR(TIMER0_OVF_vect) launch not work too, but i copy this from youtube video for test(https://youtu.be/vl5H_Q1slYY?t=1531).
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define LED1 0
#define LED2 1
int main(void)
{
TCCR0B = (1<<CS00) | (1 << CS02); //8Mhz/1024/255
TIMSK0 = (1<<TOIE0);
DDRA = 0xff;
PORTA = 0b00000100;
sei();
while(1)
{
}
}
ISR(TIMER0_OVF_vect)
{
PORTA ^= (1<<LED1);
}
#define F_CPU 16000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/io.h>
#include <stdio.h>
#define SREG _SFR_IO8(0x3f)
unsigned char OVF_counter, zamer_1, zamer_T, zamer_t, duty;
unsigned long t, T, T1, f, num;
unsigned int rising_1, rising_2, falling;
ISR(TIMER4_OVF_vect)
{
PORTA = 0b00001111;
}
ISR(TIMER0_OVF_vect)
{
PORTA = 0b00000011;
}
ISR(TIMER4_CAPT_vect)
{
}
void init_timer (void)
{
TIMSK4 |= (1 << ICIE4)|(1 << TOIE4);
TCCR4B |= (1 << ICNC4)|(1 << CS42);
TIFR4 |= (1 << TOV4);
TCCR0B = (1 << CS01);
TIFR0 |= (1 << TOV0);
SREG = 0b10000000;
SREG = 0x80;
}
void capture (void)
{
PORTB = 0b00001000;
_delay_ms(10);
PORTB = 0x00;
rising_1 = ICR4;
T=TCNT4;
TCCR4B &= ~(1 << ICES4);
falling = ICR4;
T1=TCNT4;
TCCR4B |= (1 << ICES4);
t = falling - rising_1 ;
}
int main(void)
{
SREG = 0x80;
DDRL = 0x00;
DDRB = 0xff;
DDRA = 0xff;
init_timer();
while (1)
{
capture();
if(t >= 1000000)
{
PORTA = 0b00000000;
}
if(t >= 100000)
{
PORTA = 0b00000111;
}
else if(t >= 1)
{
PORTA = 0b00000011;
}
else if(t >= 0)
{
PORTA = 0b00000001;
}
else
{
PORTA = 0b00001111;
num=0;
}
}
}

Terminal Emulator Receiving Bytes from Unknown Source via UART ATmega328p

I am playing around with a DFRobot Romeo board that has an ATmega328p MCU on it.
I wrote a program to transmit the binary and decimal representation of 4 DIP switches wired as inputs into PORTD (PD2 -> PD5). UART is wired to PORTD, PD0 (Rx) and PD1 (Tx).
The program works as expected except for when the program first runs the terminal emulator I am using on my Mac appears to receive and display the following binary/decimal number: 00000011 3, then 00000000 0 appears, or whatever number is set by the DIP switches.
If I reset the controller with the UART connection maintained, I get the same result over and over. The terminal emulator I am using is CoolTerm. I get the same result even if I flush the buffer before reconnecting. The same result is occurring if I try another terminal emulator (i.e. Serial, SerialTools).
I reviewed my code, and do not see any obvious errors. I contacted a peer who says he has seen this behaviour as well, but has no suggestions for a solution other than clearing the terminal buffer. I have also tried a few different physical configurations such as powering the board by USB only, powering the board from a separate power supply without the USB programming cable connected, and with the USB cable connected. I get the same results on the Terminal.
Can someone take a look at this and offer a suggestion as to why this behaviour is occurring?
Here is the C code for the program.
/*
Connection Diagram:
Serial USART:
Atmega328p Romeo Board FTDI Cable
PD0 -> D0 -> Orange Rx
PD1 -> D1 -> Yellow Tx
GND -> GND -> Black Ground
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Digital I/O:
Atmega328p Romeo Board IO Board Jumper Component
PD2 -> D2 -> JP2_7 DS1 Dip Switch 1 - MSB
PD3 -> D3 -> JP2_6 DS2 Dip Switch 2
PD4 -> D4 -> JP3_2 DS3 Dip Switch 3
PD5 -> D5 -> JP2_5 DS4 Dip Switch 4 - LSB
*/
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#define F_CPU 16000000UL
#define BAUD 19200
#define DOUBLE_SPEED 0
#define DECIMAL 10
//functions
void initUART(unsigned int baud, unsigned int speed);
void initDIO(void);
void printDec(int sum);
void printByte(uint8_t num);
void printCR(void);
void delay_ms (uint16_t ms);
int main(void)
{
//initialize UART
initUART(BAUD, DOUBLE_SPEED);
//initialize DIO (Digital I/O)
initDIO();
//value to be determined
uint8_t value = 0;
//previous value
uint8_t previous = 0;
while(1)
{
previous = value;
//Set or Clear LSB
if((PIND & 0b00100000) == 0)
{
value |= (1 << 0);
}
else
{
value &= ~(1 << 0);
}
//Set or Clear 2nd bit
if((PIND & 0b00010000) == 0)
{
value |= (1 << 1);
}
else
{
value &= ~(1 << 1);
}
//Set or Clear 3rd bit
if((PIND & 0b00001000) == 0)
{
value |= (1 << 2);
}
else
{
value &= ~(1 << 2);
}
//Set or Clear MSB
if((PIND & 0b00000100) == 0)
{
value |= (1 << 3);
}
else
{
value &= ~(1 << 3);
}
//if value has changed since previous, print the result
if(value != previous)
{
printByte(value);
printDec(value);
printCR();
}
//add small delay to loop in attempt to stablize the button state (debounce buttons)
delay_ms(80);
}
}
void initUART(unsigned int baud, unsigned int speed)
{
unsigned int ubrr;
//double speed is OFF in this lab (it is 0)
if(speed)
{
//double rate mode
ubrr = F_CPU/8/baud-1;
//set double speed mode
UCSR0A = (speed << U2X0);
}
else
{
//normal rate mode
ubrr = F_CPU/16/baud-1;
}
//set the baud rate
UBRR0H = (unsigned char)(ubrr >> 8);
UBRR0L = (unsigned char)(ubrr);
//enable Tx and Rx pins on MCU
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//set control bits, 8 bit char, 0 stop, no parity
UCSR0C = (1 <<UCSZ00) | (1 <<UCSZ01);
}
void initDIO(void)
{
//set inputs for Port B
//PD2 (D2) DS1
DDRD &= ~(1 << PD2);
//PD3 (D3) DS2
DDRD &= ~(1 << PD3);
//PD4 (D4) DS3
DDRD &= ~(1 << PD4);
//PD5 (D5) DS4
DDRD &= ~(1 << PD5);
//Set PORTD PD2 & PD3 & PD4 & PD5 Pull-Up Resistors
PORTD |= ((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5));
}
void printDec(int sum)
{
//character buffer for integer to string converstion
char buffer[sizeof(int)*8+1];
//convert integer to decimal represented string
itoa(sum, buffer, DECIMAL);
//transmit character string via UART
for(int i = 0; i < strlen(buffer); i++)
{
// Wait for empty transmit buffer
while( !(UCSR0A & (1 << UDRE0)) ) {};
//start transmission of character string
UDR0 = buffer[i];
}
}
void printByte(uint8_t num)
{
//transmit binary characters via UART
for(int i = 7; i >= 0; i--)
{
// Wait for empty transmit buffer
while( !(UCSR0A & (1 << UDRE0)) ) {};
//start transmission of character string.
//can add character 'O' or integer 48 to statement below,
//both result in the correct character being transmitted.
UDR0 = ((num >> i) & 1) + '0';
}
//transmit one white space
//Wait for empty transmit buffer
while( !(UCSR0A & (1 << UDRE0)) ) {};
UDR0 = 32;
}
void printCR(void)
{
//transmit carriage return
//Wait for empty transmit buffer
while( !(UCSR0A & (1 << UDRE0)) ) {};
UDR0 = 13;
//transmit line feed
//Wait for empty transmit buffer
while( !(UCSR0A & (1 << UDRE0)) ) {};
UDR0 = 10;
}
void delay_ms (uint16_t ms)
{
uint16_t i;
for (i = 0; i < ms; i++)
_delay_ms(1);
}
Here is a screen shot of what I see on the program first scan.
Maybe there is not enough time between enabling the weak pull-ups and the first sampling.
Try to put a delay before the loop!

how to use AD convertion in Atmega 1281

hi guys im trying alot to make ADC as an input pin in my Atmega1281 controller but no success
all i want is to control a micro servo with a potentiometer
here is my code please assist me with this it wont work on my controller
#define F_CPU 16000000 //16MHz
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
//#include <stdio.h>
//#include "uart/uart.h"
#define PWM_PRESCALLER 64
#define ICR_MAX (long double)F_CPU/PWM_PRESCALLER/50
#define OCR_MIN ICR_MAX/20
#define OCR_MAX ICR_MAX/10
//char uart_s[150];
volatile unsigned long adc_val=0;
volatile unsigned long counter=0;
unsigned long curr_adc=0;
ISR (ADC_vect)
{
adc_val += ADC;
counter++;
}
int main(void)
{
//uart_init(uart_calc_ubrr(F_CPU));
DDRB |= (1<<DDB1);
ICR1 = ICR_MAX;
OCR1A = OCR_MIN;
TCCR1A = (1 << COM1A1) | (1<<WGM11);
TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS11) | (1<<CS10);
ADMUX = (1 << REFS0);
ADCSRA = (1 << ADEN)|(1 << ADFR)|(1 << ADIE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
sei();
ADCSRA |= (1<<ADSC);
while(1)
{
if(counter >= 500)
{
cli();
unsigned long round_val = round(adc_val / counter);
adc_val=0;
counter=0;
if(abs(round_val - curr_adc) > 1)
{
curr_adc = round_val;
}
long double ocr = OCR_MIN + ((long double)curr_adc * (OCR_MAX - OCR_MIN)/1024);
/*sprintf(uart_s, "ADC = %8lu, OCR = %f, rounded = %d\r\n", curr_adc, ocr, (int)round(ocr));
uart_send_string((unsigned char*) uart_s);*/
OCR1A = (int)round(ocr);
//_delay_ms(100);
sei();
}
}
}

Atmega16 USART initialization

I can't for the life of me find why this code is not working to send a byte to my computer terminal window. It receives and works fine when I omit that line though. Receiving and sending also work seperately. Please note that this is over rs485 so I must disable TXEN if I want anything received to make it through the line driver ic.
#define F_CPU 7800000UL // 7.8 MHz
#include <avr/io.h>
//#include <util/delay.h>
#include <avr/interrupt.h>
#include <math.h>
#define USART_BAUDRATE 57600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
volatile unsigned int value = 0;
volatile int recievearray[4] = {0,0,0,0};
volatile int x=0;
void USART_Init(void)
{
DDRD = 0x7E; //you have to assign TXEN as output for some reason
//PORTD = 0xFF;
UCSRB = 0;
//Put the upper part of the baud number here (bits 8 to 11)
UBRRH = (unsigned char) (BAUD_PRESCALE >> 8);
//Put the remaining part of the baud number here
UBRRL = (unsigned char) BAUD_PRESCALE;
//PORTD = 2;
// ASYNCRONOUS
UCSRC = (0 << UMSEL);
//Enable the receiver and transmitter
UCSRB = (1 << RXEN) | (1 << TXEN) | (0 << UCSZ2) | (1<<RXCIE);
//Set 1 stop bits and data bit length is 8-bit
UCSRC = (1 << URSEL) | (0 << USBS) | (3 << UCSZ0);
//no parity
UCSRC |= (0 << UPM1);
UCSRC |= (0 << UPM0);
}
void USART_SendByte(uint8_t u8Data)
{
UCSRB = (1 << TXEN) | (1<<TXCIE);
// Wait until last byte has been transmitted
while((UCSRA &(1<<UDRE))==0);
UDR = u8Data;
UCSRB = (0 << TXEN) | (0<<TXCIE);
}
void Led_init(void){
DDRC = 0xFF;
}
ISR(USART_RXC_vect)
{
//PORTC = 0xFF;
while((UCSRA &(1<<RXC)) == 0);
value = UDR;
if (x < 4)
{
recievearray[x++] = value;
}
else
{
x = 0;
recievearray[x++] = value;
PORTC = 0x00;
}
}
int main(void)
{
USART_Init();
sei();
Led_init();
//PORTC = 0xFF;
for(;;)
{
if (recievearray[0] + recievearray[1] + recievearray[2] + recievearray[3] > 100)
{
PORTC = 0xFF;
USART_SendByte(value);
}
//_delay_ms(1000);
}
}

Resources