Last active
August 29, 2015 13:56
-
-
Save gerow/9106534 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************************* | |
* at328-0.c - Demonstrate simple I/O functions of ATmega328 | |
* | |
* Program loops turning PC0 on and off as fast as possible. | |
* | |
* The program should generate code in the loop consisting of | |
* LOOP: SBI PORTC,0 (2 cycles) | |
* CBI PORTC,0 (2 cycles) | |
* RJMP LOOP (2 cycles) | |
* | |
* PC0 will be low for 4 / XTAL freq | |
* PC0 will be high for 2 / XTAL freq | |
* A 9.8304MHz clock gives a loop period of about 600 nanoseconds. | |
* | |
* Revision History | |
* Date Author Description | |
* 09/14/12 A. Weber Initial Release | |
* 11/18/13 A. Weber Renamed for ATmega328P | |
*************************************************************/ | |
#include <avr/io.h> | |
#include <stdio.h> | |
#include <util/delay.h> | |
void usart_init(unsigned short ubrr) | |
{ | |
UBRR0 = ubrr; | |
UCSR0B |= (1 << TXEN0); | |
UCSR0B |= (1 << RXEN0); | |
UCSR0C = (3 << UCSZ00); | |
} | |
void usart_out(char ch) | |
{ | |
while ((UCSR0A & (1 << UDRE0)) == 0); | |
UDR0 = ch; | |
} | |
char usart_in() | |
{ | |
while ( !(UCSR0A & (1 << RXC0)) ); | |
return UDR0; | |
} | |
void usart_outstring(char *s) { | |
while (*s != '\0') { | |
usart_out(*s); | |
s++; | |
} | |
} | |
int main(void) | |
{ | |
/* for 9600 baud on with 9.304MHz clock */ | |
usart_init(63); | |
char buf[256]; | |
buf[0] = '\0'; | |
/* | |
for (;;) { | |
sprintf(&buf, "UCSR0A is %d\r\n", UCSR0A); | |
usart_outstring(buf); | |
sprintf(&buf, "RXC0 is %d\r\n", UCSR0A); | |
usart_outstring(buf); | |
} | |
*/ | |
/* | |
for(;;) { | |
char c = usart_in(); | |
usart_out(c); | |
} | |
*/ | |
for (;;) { | |
char c = usart_in(); | |
switch (c) { | |
case 'a': | |
case 'e': | |
case 'i': | |
case 'o': | |
case 'u': | |
sprintf(&buf, "You entered the vowel \"%c\"\r\n", c); | |
usart_outstring(buf); | |
break; | |
default: | |
sprintf(&buf, "You entered the consonant \"%c\"\r\n", c); | |
usart_outstring(buf); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment