AVR microcontroller serial port operation program

Publisher:自在逍遥Latest update time:2015-01-16 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include
#include
#include
#include
#include
 
 
#define FOSC 8000000 //MCU working clock frequency
 
 
typedef unsigned int uint16;
typedef unsigned char uchar8;
 
 
uchar8 DoubleIRData[25]={0x64,0x00,0x20,0x04,0x40,0x00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //Dual infrared emission data takes PD2 as the driver for remote infrared emission
//uchar8 SingleIRData[25]={0x60,0x00,0x04,0x00,0x04,0x00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} //Use PD2 as the driver for remote infrared transmitter //Use PD3 as the driver for infrared transmitter
 
 
uchar8 BitCount=0; //Carrier count
uchar8 BetyCount=0; //bit count
volatile uchar8 UsartDataBuff=0;
volatile uchar8 ChargeFlag=0,LED_Count;
volatile uchar8 SYSCount=0;
volatile uint16 ADCCurrent1=0; //AD0 conversion result voltage detection
volatile uint16 ADCCurrent3=0; //AD3 result conversion current detection
 
 
void Port_Init(void)
{
DDRA = 0b00000000; //AD sampling PA0 PA1
PORTA = 0b00000000; //PA port: initialize 0
//PB PC not used
DDRD = 0b11111110; //PD2 is the remote infrared transmitter PD3 and PD4 are status indicators, P3 is not charged P4 indicates charging PD5 and PD6 are dual infrared near-range transmitters PD7 switch charging enable
PORTD = 0b00000011; //Initialize the output to 0, the USART receiving port is pulled high to wait for the receiving interrupt
 
 
}
 
 
void ADC_Init(void)
{
ADCSRA=0x00; //ADC control status register
ADMUX = 0x40; //ADC multiplexing selection register reference voltage is AVCC, where AREF pin plus filter capacitor data right-aligned result selects channel 0
ACSR = (1<
ADCSRA=(1<ADSC)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0); //AD enable, disable automatic trigger, interrupt flag, 64 frequency division, AD interrupt disabled, ADC conversion started
}
 
 
 
 
 
 
uint16 ADC_Read(unsigned char Channelx)
{
 
 
uint16 ADTempValue;
ADMUX = 0xc0|(Channelx&0x0f); //ADMUX = 0xe0|Channelx; //2.56V on-chip reference voltage source, AREF pin plus filter capacitor data right-aligned result
ADCSRA|=(1<
loop_until_bit_is_set(ADCSRA,ADIF); //Wait for the AD conversion to finish. In fact, you can also use the interrupt method to read
ADCSRA|=(1<
ADTempValue = ADC&0x3ff; //ADC=0000 00ADCH ADCL 共16
  return ADTempValue ;
}
 
 
 
 
void Timer0_Init(void)
{
TCCR0 = 0x00; //Timer 0 control register, stop the timer when initializing
TCNT0 = 0x00; //Initial value of the counter
OCR0 = 210; //Set the comparison value to 210. The 8M clock is about 38K to achieve infrared transmission.
TIMSK|= 0x02; //enable compare match interrupt
}
 
 
void Timer2_Init(void)
{
TCCR2 = 0x00; //Stop the timer
TCNT2 = 203; //initial value (1/8000000)*(255-203)=0.0000065
TIMSK |= 0x40; 
}
 
 
void Timer1_Init(void)
{
TCCR1A = 0x00;
TCCR1B = 0x0b;
OCR1AH = 0xf4;
OCR1AL = 0x24;
TIMSK |= 0x10;
}
 
 
 
 
 
 
void Usart1_Init(uint16 baud)
{
UCSRB=0;
//UCSRA is the default value
UBRRL=(FOSC/16/baud-1)%6;//UBRRH = ((uchar8 *)(&baud))[1];
UBRRH=(FOSC/16/baud-1)/256;//UBRRL = ((uchar8 *)(&baud))[0];
UCSRC |=(1<
UCSRC &= ~((1<
// UCSRC |=(3<
UCSRB |= (1<
}
 
 
 
 
//uchar8 Usart_Read(void)
//{
 
 
// UCSRB |= (1<
// loop_until_bit_is_set(UCSRA,RXC);//Wait for Usart to receive successfully
// return UDR;
//}
ISR(SIG_USART_RECV)
{
UCSRB &= ~(1<
UsartDataBuff=UDR;
 
 
UCSRB |= (1<
 
 
}
 
 
 
 
ISR(TIMER0_COMP_vect)
{
PORTD=(PIND&0x9b)|DoubleIRData[BetyCount];
TCCR2  = 0x01;
}
 
 
 
 
 
 
ISR(TIMER2_OVF_vect)
{
PORTD=PIND&0x9F;
TCCR2  = 0;
TCNT2  = 203;
BitCount++;
if(BitCount>=66) //Baud rate = 1/(0.00002625S*66) = 577
{
BetyCount++;
if(BetyCount>=20)//25
{
//TCCR0=0;
//TCCR2=0;
BetyCount=0;
}
BitCount=0;
}
}
 
 
ISR(TIMER1_COMPA_vect)
{
SYSCount=1;
}
 
 
void Usart_send(uchar8 data)
{
while(!(UCSRA&(1<
UDR=data;
}
 
 
int main(void)
{
MCUCR=0x00;
Port_Init();
Timer0_Init();
Timer1_Heat();
Timer2_Heat();
ADC_Init();
Usart1_Init(581);//581
sei(); //Open global interrupt and close all interrupts cli()
TCCR0  = 0x09;
TCCR2  = 0x01;
while(1)
{
 
 
if(SYSCount==1)
{
Usart_send(UsartDataBuff);
SYSCount=0;
if(ChargeFlag==1)
{
PORTD|=(1<
PORTD&=~(1<
}
else
{
PORTD&=~(1<
PORTD|=(1<
}
if((PORTD&0x80)==0x80)//Charging
{
ADCCurrent1=ADC_Read(1); //voltage detection
ADCCurrent3=ADC_Read(3); //Channel 3 is not in accordance with the circuit. This is mainly because channel 0 on the experimental board is damaged, so jumper 3 is set. Correction time: 2014.5.28 
if(ADCCurrent3==0)//The current is 0, stop power supply//0
{
PORTD&=~(1<
ChargeFlag=0;
}
}
else
{
ADCCurrent1=ADC_Read(1);
if(ADCCurrent1>=200)//voltage is greater than start power supply//200
{
PORTD|=(1<
ChargeFlag=1;
}
else
{
ChargeFlag=0;
}
}
 
 
//UsartDataBuff=Usart_Read();
if(UsartDataBuff)
{
}
}
//i=((uchar8 *)(&k))[0];
//j=((uchar8 *)(&k))[1];
}
}
Keywords:AVR Reference address:AVR microcontroller serial port operation program

Previous article:Error handling when using IAR and precise delay of AVR
Next article:AVR atmega16 protocol development

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号