• Atmel Atmega168









ADC

/* -----------------------------------------------------------------------
 
 * Title: Analog read example (Analog 8bits input(PC2) PORTD output)
 
 * Hardware: Atmega168
 
 * Software: WinAVR 20060421
 
 * -----------------------------------------------------------------------*/
 
 
 
#define F_CPU 8000000 // Define software reference clock for delay duration
 
 
 
#include <avr/io.h>
 
#include <util/delay.h>
 
 
 
#define LED PORTD // Define LED on 8 bits PORTD
 
 
 
char adc()
 
{
 
 ADCSRA |= (1 << ADEN); // Analog-Digital enable bit
 
 
 
 ADCSRA |= (1 << ADSC); // Discarte first conversion
 
 while (ADCSRA & (1 << ADSC)); // wait until conversion is done
 
 
 
 ADCSRA |= (1 << ADSC); // start single conversion
 
 while (ADCSRA & (1 << ADSC)) // wait until conversion is done
 
 
 
 ADCSRA &= ~(1<<ADEN); // shut down the ADC
 
 
 
return ADCH;
 
}
 
 
 
 
 
int main(void)
 
 
 
{
 
 
 
DDRD = 0xFF; // Set output direction on PORTB
 
ADCSRA |= (1 << ADEN) | // Analog-Digital enable bit
 
 (1 << ADPS1)| // set prescaler to 8 (clock / 8)
 
 (1 << ADPS0); // set prescaler to 8 (doc Atmega168 23.8.2 p.258)
 
 
 
ADMUX |= (1 << ADLAR)| // AD result store in (more significant bit in ADCH)
 
 (1 << MUX1); // Choose AD input AD2 (PC2)
 
 
 
 for (;;)
 
 {
 
 LED = adc(); // PORTD output = Analog input (8bits)
 
 }
 
 
 
return 0;
 
}
 
 

PWM High-Speed

/* -----------------------------------------------------------------------
 
 * Title: PWM High Speed (Analog 8bits input(PC2) PWM-HS output PD6)
 
 * Hardware: Atmega168
 
 * Software: WinAVR 20060421
 
 * -----------------------------------------------------------------------*/
 
 
 
#include <avr/io.h>
 
 
 
char adc()
 
{
 
 ADCSRA |= (1 << ADEN); // Analog-Digital enable bit
 
 
 
 ADCSRA |= (1 << ADSC); // Discarte first conversion
 
 while (ADCSRA & (1 << ADSC)); // wait until conversion is done
 
 
 
 ADCSRA |= (1 << ADSC); // start single conversion
 
 while (ADCSRA & (1 << ADSC)) // wait until conversion is done
 
 
 
 ADCSRA &= ~(1<<ADEN); // shut down the ADC
 
 
 
return ADCH;
 
}
 
 
 
// ************************** init timer ****************************************************
 
 
 
void InitPWM()
 
{
 
 
 
DDRD = 0x40; // Set direction output on PD6 (OC0A)
 
 
 
TCCR0B |= (1<<CS00); // Timer2 Settings: Timer Prescaler /1,
 
 
 
TCCR0A |= ((1<<WGM00)|(1<<WGM01)| // Use H-Speed PWM mode
 
 (1<<COM0A1)|(1<<COM0A0));
 
 
 
}
 
 
 
// **************************** Init ADC ****************************************************
 
 
 
void InitADC()
 
{
 
 
 
ADCSRA |= (1 << ADEN) | // Analog-Digital enable bit
 
 (1 << ADPS1)| // set prescaler to 8 (clock / 8)
 
 (1 << ADPS0); // set prescaler to 8 (doc Atmega168 23.8.2 p.258)
 
 
 
ADMUX |= (1 << ADLAR)| // AD result store in (more significant bit in ADCH)
 
 (1 << MUX1); // Choose AD input AD2 (PC2)
 
 
 
}
 
 
 
int main() {
 
 
 
InitPWM();
 
InitADC();
 
 
 
 for (;;)
 
 {
 
 OCR0A = adc(); // PWM output = ADC input
 
 }
 
 
 
return 0;
 
}
 
 
 
 
ADC free running mode

#define F_CPU 8000000 // Define software reference clock for delay duration
 
#include <avr/io.h>
 
#include <util/delay.h>
 
 
 
#define LED PORTD // Define LED on 8 bits PORTD
 
 
 
int main(void)
 
 
 
{
 
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128
 
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
 
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
 
 
 
ADCSRA |= ((1 << ADEN)|(1<<ADATE)); // Enable ADC + auto triger
 
ADCSRA |= (1 << ADSC); // Start A2D Conversions
 
 
 
DDRD = 0xFF;
 
 for (;;)
 
 {
 
LED = ADCH;
 
_delay_ms(2);
 
 }
 
 
 
return 0;
 
}
 
 
 
 

/* -----------------------------------------------------------------------
 
 * Title: Configuration du USART RxTx (8 bit + 1 stop bit 34800)
 
 * Hardware: ATmega168 (20Mhz)
 
 * Software: WinAVR 20060421
 
 -----------------------------------------------------------------------*/
 
 
 
#define F_CPU 16000000UL // La fréquence de référence (pour delay.h)
 
#include <avr/io.h> // Un include pour les fonctions d'entrés/sortie
 
#include <util/delay.h>
 
#include <util/stdlib.h>
 
#include <avr/interrupt.h>
 
 
 
#define FOSC 16000000 // Clock Speed
 
#define BAUD 38400 // Baud rate
 
#define MYUBRR FOSC/16/BAUD-1 // Calcule magique pour configurer la communication
 
 
 
// ********************************* Initialisation du USART *********************************
 
 
 
unsigned char String[] = "Hello word!!!";
 
 
 
void USART_Init( unsigned int ubrr)
 
{
 
 
 
UBRR0H = (unsigned char)(ubrr>>8); // | Charger la vitesse de communication
 
UBRR0L = (unsigned char)ubrr; // | UBRR0H 8bit avec UBRR0L 8b = ensemble ubrr 18 bit
 
 
 
UCSR0B = (1<<RXEN0)|(1<<TXEN0); // Rx Tx enable
 
 
 
UCSR0C = (3<<UCSZ00); // Set frame format: 8data, 1 stop bit
 
 
 
}
 
 
 
// ************************************* USART Envois ****************************************
 
 
 
void USART_Transmit( unsigned char data )
 
{
 
 
 
while ( !( UCSR0A & (1<<UDRE0)) ); // Attendre que le buffer se vide
 
 
 
UDR0 = data; // Envoyer le Data
 
 
 
}
 
 
 
// ************************************* USART Reception **************************************
 
 
 
unsigned char USART_Receive( void )
 
{
 
 
 
while ( !(UCSR0A & (1<<RXC0)) ); // Attendre la reception du Data
 
 
 
 
 
return UDR0; // Retour de fonction
 
}
 
 
 
// ************************************* MAIN LOOP **************************************
 
 
 
int main( void )
 
{
 
 
 
USART_Init ( MYUBRR ); // initialisation de du baud rate (selon le calcule magique)
 
 
 
 
 
volatile int x;
 
 
 
 for(;;) // super loop
 
 {
 
 
 
 for (x = 0 ; x < 13 ; x++)
 
 {
 
 
 
 USART_Transmit(String[x]); // Envoyer 65 (format decimal) sur le port Tx
 
 }
 
 
 
 
 
 USART_Transmit(13); // Enter
 
 
 
 }
 
 
 
return 0;
 
}
 
 
 
 

//-----------------------------------------------------------------------------
 
// Junk for REF atmega168168
 
//-----------------------------------------------------------------------------
 
 
 
#define F_CPU 1600000UL
 
#include <avr/io.h>
 
#include <util/delay.h>
 
#include <stdio.h>
 
#include <avr/interrupt.h>
 
 
 
 
 
#define FOSC 16000000
 
#define BAUD 56000
 
#define UBRR FOSC/16/BAUD-1
 
 
 
#define cSEP 47
 
volatile char cString[12] = "test";
 
volatile char cString2[12];
 
 
 
volatile int counter;
 
volatile char ASCI_value[12];
 
 
 
static int uart_putchar(char c, FILE *stream);
 
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
 
 
 
 
 
//-----------------------------------------------------------------------------
 
// Initialize USART0
 
//-----------------------------------------------------------------------------
 
void init_USART0(unsigned int baud)
 
{
 
 UBRR0 = baud; // Set Baudrate
 
 UCSR0C = (3<<UCSZ00); // Character Size 8 bit
 
 UCSR0B |= _BV(RXEN0) | _BV(TXEN0); // Receiver and Transmitter Enable
 
}
 
 
 
static int uart_putchar(char c, FILE *stream)
 
{
 
 if (c == '\n') uart_putchar('\r', stream);
 
 
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = c;
 
 
 
 return 0;
 
}
 
 
 
 
 
 
 
int CHK_valid_string(void)
 
{
 
 int i;
 
 int error = 0;
 
 int CHK_dscore = 0; // check for "_"
 
 int CHK_space = 0; // check for " "
 
 
 
 
 
 for (i = 0 ; i < 16 ; i++)
 
 {
 
 if (cString[i]== cSEP) CHK_dscore++;
 
 if (cString[i]== 32) CHK_space++;
 
 }
 
 
 
 if ((CHK_dscore==0)|(CHK_dscore > 2 )|(CHK_space >= 1 )) error = 1;
 
 
 
 return error;
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Transmit String Data
 
//-----------------------------------------------------------------------------
 
void transmit_str_USART0(char *str)
 
{
 
 while (*str != 0)
 
 {
 
 transmit_1byte_USART0(*str);
 
 *str++;
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Receive 1 byte Data
 
//-----------------------------------------------------------------------------
 
unsigned char receive_1byte_UART0(void)
 
{
 
 loop_until_bit_is_set(UCSR0A, RXC0);
 
 return UDR0;
 
}
 
 
 
void receive_string(void)
 
{
 
 
 
 
 
 cString[counter]= receive_1byte_UART0();
 
 
 
 if (cString[counter] == '\r') { // check for RETURN (also need to deal with linefeed)
 
 
 
 //if (CHK_valid_string()== 0 )
 
 // {
 
 
 
 
 
 
 
 
 
sscanf (cString,"%s %s",cString2,ASCI_value);
 
 
 
 
 
 
 
ASCI_value[counter]=0;
 
 
 
transmit_str_USART0("First= ");
 
transmit_str_USART0(cString2);
 
transmit_1byte_USART0(13);
 
transmit_str_USART0("Second= ");
 
transmit_str_USART0(ASCI_value);
 
transmit_1byte_USART0(13);
 
transmit_1byte_USART0(13);
 
 
 
//}
 
 
 
 for (counter = 0 ; counter < 8 ; counter++) ASCI_value[counter]=0;
 
 for (counter = 0 ; counter < 8 ; counter++) cString[counter]=0;
 
 
 
 counter = -1;
 
 
 
 
 
 
 
 }
 
 
 
 else
 
 {
 
 counter++; // only increment if not end of string
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Transmit 1 byte Data
 
//-----------------------------------------------------------------------------
 
void transmit_1byte_USART0(unsigned char data)
 
{
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = data;
 
}
 
 
 
 
 
//-----------------------------------------------------------------------------
 
// TAG SEND
 
//-----------------------------------------------------------------------------
 
 
 
TAG_send(char tag[8], int value)
 
{
 
 
 
printf ("%s %d\n",tag,value);
 
 
 
}
 
 
 
 
 
 
 
 
 
 
 
//-----------------------------------------------------------------------------
 
// Main
 
//-----------------------------------------------------------------------------
 
int main()
 
{
 
 init_USART0(UBRR); // initialize USART0
 
 stdout = &mystdout; //Required for printf init
 
 
 
 UCSR0B |= (1 << RXCIE0); // Enable the USART Recieve Complete interrupt (USART_RXC)
 
 sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
 
 
 
 for(;;)
 
 {
 
 
 
 
 
//_delay_ms(2000);
 
//TAG_send("test", 34);
 
// receive_string();
 
 
 
 }
 
 
 
 return 0;
 
}
 
 
 
ISR(USART_RX_vect)
 
{ cString[counter]= receive_1byte_UART0();
 
 
 
 if (cString[counter] == '\r') { // check for RETURN (also need to deal with linefeed)
 
 
 
 //if (CHK_valid_string()== 0 )
 
 // {
 
 
 
 
 
 
 
 
 
 sscanf (cString,"%s %s",cString2,ASCI_value);
 
 
 
 
 
 
 
ASCI_value[counter]=0;
 
 
 
transmit_str_USART0("First= ");
 
transmit_str_USART0(cString2);
 
transmit_1byte_USART0(13);
 
transmit_str_USART0("Second= ");
 
transmit_str_USART0(ASCI_value);
 
transmit_1byte_USART0(13);
 
transmit_1byte_USART0(13);
 
 
 
//}
 
 
 
 for (counter = 0 ; counter < 8 ; counter++) ASCI_value[counter]=0;
 
 for (counter = 0 ; counter < 8 ; counter++) cString[counter]=0;
 
 
 
 counter = -1;
 
 
 
 
 
 
 
 }
 
 
 
 else
 
 {
 
 counter++; // only increment if not end of string
 
 }
 
}
 
 

/* -----------------------------------------------------------------------
 
SEnder
 
 -----------------------------------------------------------------------*/
 
 
 
#define F_CPU 8000000UL // Define software reference clock for delay duration
 
// Must be write before call delay.h
 
#include <avr/io.h>
 
#include <util/delay.h>
 
#include <stdio.h>
 
#include <avr/interrupt.h>
 
 
 
 
 
#define FOSC 20000000
 
#define BAUD 9600
 
#define UBRR FOSC/16/BAUD-1
 
#define INPUT PD2
 
 
 
 
 
volatile unsigned int NBRturn = 0;
 
static int uart_putchar(char c, FILE *stream);
 
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
 
 
 
//-----------------------------------------------------------------------------
 
// Initialize USART0
 
//-----------------------------------------------------------------------------
 
void init_USART0(unsigned int baud)
 
{
 
 UBRR0 = baud; // Set Baudrate
 
 UCSR0C = (3<<UCSZ00); // Character Size 8 bit
 
 UCSR0B |= _BV(RXEN0) | _BV(TXEN0); // Receiver and Transmitter Enable
 
}
 
 
 
static int uart_putchar(char c, FILE *stream)
 
{
 
 if (c == '\n') uart_putchar('\r', stream);
 
 
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = c;
 
 
 
 return 0;
 
}
 
 
 
void uPause(long timer)
 
{
 
 int x;
 
 for(x = 0 ; x < timer ; x++)
 
 {
 
 _delay_ms(1);
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Transmit 1 byte Data
 
//-----------------------------------------------------------------------------
 
void transmit_1byte_USART0(unsigned char data)
 
{
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = data;
 
}
 
 
 
 
 
//-----------------------------------------------------------------------------
 
// TAG SEND
 
//-----------------------------------------------------------------------------
 
 
 
void TAG_send(char tag[8], int value)
 
{
 
 
 
 printf ("%s %0.5d",tag,value);
 
transmit_1byte_USART0(13);
 
 
 
}
 
 
 
// ************************************** INT INIT ********************************************
 
void IOinit(void)
 
{
 
 SREG = 0x01;
 
 
 
 EICRA = (1 << ISC01); // external interrup PD2 (pin 4)
 
 EIMSK = (1 << INT0);
 
 
 
 sei(); // int - Global enable interrupts
 
 
 
}
 
 
 
int main(void) {
 
 
 
 
 
stdout = &mystdout; //Required for printf init
 
IOinit();
 
 
 
SREG = (1<<SREG_I);
 
TCCR1B|= ( 1 << WGM12); // Configure timer 1 for CTC mode
 
TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt
 
 
 
OCR1AH = 37; // Set CTC compare value
 
OCR1AL = 255; // Set CTC compare value
 
 
 
TCCR1B |= (1 << CS12| 1 << CS10); // Start timer at Fcpu/64
 
 
 
 
 
init_USART0(UBRR); // initialize USART0
 
 
 
DDRB &= ~(1<<INPUT);
 
 
 
 int x;
 
 
 
 
 
 for (;;) // Endless loop
 
 
 
 
 
 {
 
 
 
 }
 
 
 
 return 0;
 
}
 
 
 
ISR(TIMER1_COMPA_vect)
 
{
 
 
 
 TAG_send("Num", (NBRturn * 2 ));
 
NBRturn =0;
 
}
 
 
 
ISR(INT0_vect) // Interrupt on Int0 vector
 
{
 
 
 
 NBRturn++;
 
 
 
//transmit_1byte_USART0(18);
 
 
 
}
 
 
 
 
 
 
 
***********************************
 
 
 
/* -----------------------------------------------------------------------
 
 * Title: Led blinking reaction on pressed switch (I/O)
 
 * Hardware: ATtiny13
 
 * Software: WinAVR 20060421
 
 -----------------------------------------------------------------------*/
 
 
 
#define F_CPU 20000000UL // Define software reference clock for delay duration
 
// Must be write before call delay.h
 
#include <avr/io.h>
 
#include <util/delay.h>
 
#include <stdio.h>
 
#include <avr/interrupt.h>
 
 
 
 
 
#define FOSC 20000000
 
#define BAUD 9600
 
#define UBRR FOSC/16/BAUD-1
 
 
 
 
 
volatile char cString[12];
 
volatile char cString2[12];
 
volatile unsigned char NUMBER[] = "00000";
 
 
 
#define LED1 PB1 // Define ext led pin on PB0 *2
 
#define LED2 PB0 // Define ext led pin on PB0 *2
 
 
 
#define SYNC PD2
 
 
 
#define LED3 PD3
 
#define LED4 PD4
 
#define LED5 PD5
 
#define LED6 PD6
 
#define LED7 PD7
 
 
 
#define refresh_rate 300
 
#define sync_rate 200
 
 
 
volatile int counter = 0;
 
volatile unsigned char ASCI_value[5];
 
 
 
#define CATH1 PC0
 
#define CATH2 PC1
 
#define CATH3 PC2
 
#define CATH4 PC3
 
#define CATH5 PC4
 
 
 
int sync_count = 0;
 
 
 
static int uart_putchar(char c, FILE *stream);
 
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
 
 
 
//-----------------------------------------------------------------------------
 
// Initialize USART0
 
//-----------------------------------------------------------------------------
 
void init_USART0(unsigned int baud)
 
{
 
 UBRR0 = baud; // Set Baudrate
 
 UCSR0C = (3<<UCSZ00); // Character Size 8 bit
 
 UCSR0B |= _BV(RXEN0) | _BV(TXEN0); // Receiver and Transmitter Enable
 
}
 
 
 
static int uart_putchar(char c, FILE *stream)
 
{
 
 if (c == '\n') uart_putchar('\r', stream);
 
 
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = c;
 
 
 
 return 0;
 
}
 
 
 
 
 
void uPause(int timer)
 
{
 
 int x;
 
 for(x = 0 ; x < timer ; x++)
 
 {
 
 _delay_us(1);
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Transmit String Data
 
//-----------------------------------------------------------------------------
 
void transmit_str_USART0(char *str)
 
{
 
 while (*str != 0)
 
 {
 
 transmit_1byte_USART0(*str);
 
 *str++;
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Receive 1 byte Data
 
//-----------------------------------------------------------------------------
 
unsigned char receive_1byte_UART0(void)
 
{
 
 loop_until_bit_is_set(UCSR0A, RXC0);
 
 return UDR0;
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Receive string
 
//-----------------------------------------------------------------------------
 
 
 
void receive_string(void)
 
{
 
 
 
 
 
 cString[counter]= receive_1byte_UART0();
 
 
 
 if (cString[counter] == '\r') { // check for RETURN (also need to deal with linefeed)
 
 
 
 //if (CHK_valid_string()== 0 )
 
 // {
 
 
 
 
 
 
 
 
 
 sscanf (cString,"%s %s",cString2,NUMBER);
 
 
 
 
 
 
 
 ASCI_value[counter]=0;
 
 
 
 transmit_str_USART0("First= ");
 
 transmit_str_USART0(cString2);
 
 transmit_1byte_USART0(13);
 
 transmit_str_USART0("Second= ");
 
 transmit_str_USART0(ASCI_value);
 
 transmit_1byte_USART0(13);
 
 transmit_1byte_USART0(13);
 
 
 
 //}
 
 
 
 for (counter = 0 ; counter < 8 ; counter++) ASCI_value[counter]=0;
 
 for (counter = 0 ; counter < 8 ; counter++) cString[counter]=0;
 
 
 
 counter = -1;
 
 
 
 
 
 
 
 }
 
 
 
 else
 
 {
 
 counter++; // only increment if not end of string
 
 }
 
 
 
}
 
 
 
//-----------------------------------------------------------------------------
 
// Transmit 1 byte Data
 
//-----------------------------------------------------------------------------
 
void transmit_1byte_USART0(unsigned char data)
 
{
 
 loop_until_bit_is_set(UCSR0A, UDRE0);
 
 UDR0 = data;
 
}
 
 
 
 
 
//-----------------------------------------------------------------------------
 
// TAG SEND
 
//-----------------------------------------------------------------------------
 
 
 
void TAG_send(char tag[8], int value)
 
{
 
 
 
 printf ("%s %d\n",tag,value);
 
 
 
}
 
 
 
 
 
void SEGshow(int seg)
 
{
 
 
 
 
 
 switch (seg)
 
 {
 
 
 
 case 48:
 
 PORTB |= (1 << LED2); // 0
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD |= (1 << LED7);
 
 PORTB &= ~(1 << LED1);
 
 break;
 
 
 
 case 49:
 
 PORTB &= ~(1 << LED2); //1
 
 PORTD &= ~(1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD &= ~(1 << LED6);
 
 PORTD &= ~(1 << LED7);
 
 PORTB &= ~(1 << LED1);
 
 break;
 
 
 
 
 
 case 50:
 
 PORTB &= ~(1 << LED2); //2
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD &= ~(1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD |= (1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 
 
 case 51:
 
 PORTB &= ~(1 << LED2); //3
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD &= ~(1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 
 
 case 52:
 
 PORTB |= (1 << LED2); // 4
 
 PORTD &= ~(1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD &= ~(1 << LED6);
 
 PORTD &= ~(1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 
 
 case 53:
 
 PORTB |= (1 << LED2); // 5
 
 PORTD |= (1 << LED3);
 
 PORTD &= ~ (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD &= ~ (1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 case 54:
 
 PORTB |= (1 << LED2); // 6
 
 PORTD |= (1 << LED3);
 
 PORTD &= ~(1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD |= (1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 case 55:
 
 PORTB &= ~(1 << LED2); // 7
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD &= ~(1 << LED6);
 
 PORTD &= ~(1 << LED7);
 
 PORTB &= ~(1 << LED1);
 
 break;
 
 
 
 case 56:
 
 PORTB |= (1 << LED2); // 8
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD |= (1 << LED6);
 
 PORTD |= (1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 
 
 case 57:
 
 PORTB |= (1 << LED2); // 9
 
 PORTD |= (1 << LED3);
 
 PORTD |= (1 << LED4);
 
 PORTD |= (1 << LED5);
 
 PORTD &= ~(1 << LED6);
 
 PORTD &= ~(1 << LED7);
 
 PORTB |= (1 << LED1);
 
 break;
 
 
 
 }
 
 
 
 
 
 
 
}
 
 
 
 
 
int main(void) {
 
 
 
 
 
 DDRB |= (1 << LED1); // Set output direction on LED (PB0) *2
 
 DDRB |= (1 << LED2); // Set output direction on LED (PB0) *2
 
 DDRD |= (1 << LED3); // Set output direction on LED (PB0) *2
 
 DDRD |= (1 << LED4); // Set output direction on LED (PB0) *2
 
 DDRD |= (1 << LED5); // Set output direction on LED (PB0) *2
 
 DDRD |= (1 << LED6); // Set output direction on LED (PB0) *2
 
 DDRD |= (1 << LED7); // Set output direction on LED (PB0) *2
 
 
 
 DDRD |= (1 << SYNC); // Set output direction on LED (PB0) *2
 
 
 
 DDRC = 31;
 
 PORTC = 31;
 
 
 
 init_USART0(UBRR); // initialize USART0
 
 stdout = &mystdout; //Required for printf init
 
 
 
 UCSR0B |= (1 << RXCIE0); // Enable the USART Recieve Complete interrupt (USART_RXC)
 
 sei(); // Enable the Global Interrupt Enable flag so that interrupts can be processed
 
 
 
 
 
 
 
 int x;
 
 for (;;) // Endless loop
 
 
 
 
 
 {
 
 
 
 // TAG_send("test", 35);
 
 //uPause(1);
 
 // receive_string();
 
 x = 0;
 
 
 
 
 
 PORTC &= ~ (1 << CATH1);
 
 SEGshow(NUMBER[x]);
 
 uPause(refresh_rate);
 
 PORTC |= (1 << CATH1);
 
 x++;
 
 PORTC &= ~ (1 << CATH2);
 
 SEGshow(NUMBER[x]);
 
 uPause(refresh_rate);
 
 PORTC |= (1 << CATH2);
 
 x++;
 
 PORTC &= ~ (1 << CATH3);
 
 SEGshow(NUMBER[x]);
 
 uPause(refresh_rate);
 
 PORTC |= (1 << CATH3);
 
 x++;
 
 PORTC &= ~ (1 << CATH4);
 
 SEGshow(NUMBER[x]);
 
 uPause(refresh_rate);
 
 PORTC |= (1 << CATH4);
 
 x++;
 
 PORTC &= ~ (1 << CATH5);
 
 SEGshow(NUMBER[x]);
 
 uPause(refresh_rate);
 
 PORTC |= (1 << CATH5);
 
 
 
sync_count++;
 
if (sync_count > sync_rate)
 
{
 
 PORTD |= (1 << SYNC);
 
 PORTD &= ~(1 << SYNC);
 
sync_count =0;
 
}
 
 }
 
 
 
 return 0;
 
}
 
 
 
 
 
ISR(USART_RX_vect)
 
{
 
 
 
 cString[counter]= receive_1byte_UART0();
 
 
 
 if (cString[counter] == '\r') { // check for RETURN (also need to deal with linefeed)
 
 
 
 
 
 sscanf (cString,"%s %s",cString2,ASCI_value);
 
 
 
 
 
 
 
 if (!strcmp(cString2,"Num"))
 
 {
 
 int t;
 
 
 
 for (t = 0 ; t < ; t++)
 
 {
 
 
 
 NUMBER[t] = ASCI_value[t];
 
 }
 
 
 
 // sscanf (cString,"%s %s",cString2,NUMBER);
 
 // transmit_str_USART0(ASCI_value);
 
 // transmit_1byte_USART0(13);
 
 
 
 }
 
 
 
 // for (counter = 0 ; counter < 8 ; counter++) ASCI_value[counter]=0;
 
 // for (counter = 0 ; counter < 8 ; counter++) cString[counter]=0;
 
 
 
 counter = 0;
 
 }
 
 
 
 else
 
 {
 
 counter++; // only increment if not end of string
 
 
 
 }
 
}


For positive or negative comments please contact Open.SamSet@gmail.com