Вы находитесь на странице: 1из 5

CPE 301

April 18, 2013


Design Assignment #4
Code UART:

#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>



int main(void)

{

UCSR0B = 0x08; //enable UART transmission
UCSR0C = 0x06; //set UART char size to 8
UBRR0L = 0x33; //set baud rate to 9600


ADCSRA = 0x86; //turn on ADC select clock 1/64

while(1)
{

ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
_delay_ms(1000); //delay 1 second
unsigned char str[5] = "TEMP ";

for(int i=0; i<5; i++) //send TEMP to screen
{
while(! (UCSR0A & (1<<UDRE0)));
UDR0 = str[i];
}

double t = ADCL + (ADCH << 8);//get ADC value, convert to double
t = (t *.48) +5; //convert ADC to temp
unsigned int first = ((unsigned int)t/10) % 10;//get first digit of temp
unsigned int second = ((unsigned int)t%10);//get second digit of temp
while(! (UCSR0A & (1<<UDRE0)));
UDR0 = first + 48; //send first char to screen

while(! (UCSR0A & (1<<UDRE0)));
UDR0 = second + 48; //send second char to screen
unsigned char str2[2] = " \n";//send linefeed to screen

for(int i=0; i<2; i++)
{
while(! (UCSR0A & (1<<UDRE0)));
UDR0 = str2[i];
}
}
}

Code Flowchart:























Code SPI:


#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>

#define MOSI 3 //PB3
#define SCK 5 //PB5
#define SS 2 //PB2

void execute( unsigned char cmd, unsigned char data )
{
PORTB &= ~(1<<SS); //initialize packet


SPDR = cmd; //sende command
while( !(SPSR & (1<<SPIF)));


SPDR = data; //send data
while( !(SPSR & (1<<SPIF)));


PORTB |= (1<<SS); //terminate packet
}

Set USART to transmit 8 char
with 9600 baud rate. Turn on
ADC with clock 1/64.
Start ADC.
Wait for ADC conversion to be done.
NOT
DONE
DONE
Delay 1 second. Transmit TEMP to
screen.
Convert ADC value to Fahrenheit.
Convert Fahrenheit value to ASCII
Send first Digit to screen. Send
second Digit to screen.
Send \n to screen.



int main(void)

{


ADCSRA = 0x86; //turn on ADC select clock 1/64
DDRB = (1<<MOSI)|(1<<SCK)|(1<<SS); //set MOSI, SCK, and SS to output
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0); //enable SPI as master


execute( 0x09, 0b00000011); //enable decoding for digit 1 and 2
execute(0x0B, 0x02); //scan 2 segments
execute(0x0C, 0x01); //turn on max7221








while(1)
{



ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
_delay_ms(1000); //delay 1 second


double t = ADCL + (ADCH << 8);//get ADC value, convert to double
t = (t *.48) +5; //convert ADC to temp
unsigned int first = ((unsigned int)t/10) % 10; //get first digit of temp
unsigned int second = ((unsigned int)t%10); //get second digit of temp

execute(0x01, first ); //transmit first digit to seg 0
execute(0x02, second ); //transmit second digit to seg 1

}
}










Code Flowchart:












































Set MOSI SCK and SS to output.
Enable SPI as master. Turn on
ADC with clock 1/64.
Start ADC.
Wait for ADC conversion to be done.
NOT
DONE
DONE
Delay 1 second.
Convert ADC value to Fahrenheit.
Send first Digit to 7221. Send
second Digit to 7221.
Schematic

Вам также может понравиться