#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<
Keywords:AVR
Reference address:AVR microcontroller serial port operation program
ChargeFlag=1;
}
else
{
ChargeFlag=0;
}
}
//UsartDataBuff=Usart_Read();
if(UsartDataBuff)
{
}
}
//i=((uchar8 *)(&k))[0];
//j=((uchar8 *)(&k))[1];
}
}
Previous article:Error handling when using IAR and precise delay of AVR
Next article:AVR atmega16 protocol development
- Popular Resources
- Popular amplifiers
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
MoreDaily News
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
Guess you like
- Looking for an audio spectrum chip
- [N32L43x Review] OPAMP internal operational amplifier usage
- Oscilloscope measurement of automobile CAN-BUS bus signal and waveform analysis
- Xun uses Yocto file system to develop QT file system for i.MX6ULL Terminator
- Msp430F5438A interrupt
- 10 ways to dissipate heat from PCB!
- MSP430F5529 clock 25MHz setting method
- The impact of intermittent power charging on battery life
- Synthesizable Verilog Syntax (Cambridge University, photocopy)
- Oscilloscope measurement and waveform analysis of intake air pressure sensor signal in auto repair