AVR AD conversion programming in C language

Publisher:jiaohe1Latest update time:2016-08-06 Source: eefocusKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#define uchar unsigned char
#define uint unsigned int


uchar Table[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar Da ta[4]={0,0,0,0};

void DelayMs(uint i) //0.25ms
{uchar j;
for(;i!=0;i--)
{for(j=250;j!=0;j--) {;}}
}


void Display(uchar *p) //Display {uchar i,sel=0x01;
for(i=0;i<4;i++)
{PORTB=sel;
   PORTC=0xff-Table[p[i]];
   DelayMs at 5ms interval (20);
   sel=sel<<1;
}
}

uint ADC_Convert(void)
{uint temp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return(temp2);
}

void Process(uint i,uchar *p)
{p[0]=i/1000;
i=i%1000;
p[1]=i/100;
i=i%100;
p[2]=i/10;
i=i%10;
p[3]=i;
}

/*   
void Timer0_Init(void)
{SREG=SREG|0x80; //Global disconnect;
TIMSK=TIMSK|0x01; //T/C0 overflow interrupt enable;
TCCR0=0x05; //CLK/1024; 128uS
TIFR=0x01; //T/C0 interrupt flag write 1 to clear 0;
TCNT0=256-125; //125*128us=16ms
}*/


void main(void)
{uint i;
//Set port A to input without pull-up;
DDRA=0x00;
PORTA=0x00;
//Set port B to push-pull 1 output;
DDRB=0xff;
PORTB=0xff;
//Set port C to push-pull 1 output;
DDRC=0xff;
PORTC=0xff;
//Initialize port B to output 1; initialize port C to output 0; light up all digital tubes;
PORTB=0xff;
PORTC=0x00;
//ADC initialization;
ADMUX=0x02; //Select the third channel;
ADCSR=0xe6; //125K conversion rate, free conversion mode; start AD converter;
//Delay until the system is stable;
DelayMs(3000);

while(1)
{i=ADC_Convert();
Process(i,Data );
Display( Data);
}
}

/*#pragma interrupt_handler Timer0:10
void Timer0()
{Int_Cnt+=1;
// Timer0_Init();

}*/

Keywords:AVR Reference address:AVR AD conversion programming in C language

Previous article:AVR Eeprom C language programming
Next article:ATmega16L learning board 18B20 test program

Recommended ReadingLatest update time:2024-11-16 21:35

PIC and AVR's inherent anti-interference performance
In one of my products, there are two chips, AVR and PIC, at the same time. When AVR drives the relay, then drives the contactor. PIC is used for display. I found that PIC actually has a little interference, so I had to add measures to the peripheral circuit to solve the problem. It is said that PIC has first-class ant
[Microcontroller]
A simple AVR access control system code
After the 1602 and key program debugging, encoding, password comparison, gatekeeping, and some processing of error codes are improved, they will not be uploaded. Here I will only leave a very basic version backup for myself. #define F_CPU 4000000 #include D:\WinAVR-20090313\avr\include\avr\io.h #include D:\Win
[Microcontroller]
AVR microcontroller high-precision delay program assembly language can be used directly
The baud rate is 7.3728MHZ, the delay is 1ms, and the accuracy error is 10us ;******************************** delay_1ms:            ldi r27,41    delay_11:           ldi r28,35   delay_22:         nop         nop        dec r28       brne delay_22        dec r27      brne delay_11    ret ;***************************
[Microcontroller]
Summary of UART communication of AVR microcontroller ATMega16
#include #include macros.h typedef unsigned char uint8_t; #define DF_Config_Uart0_BaudRate 9600 //UART0 initialization // desired baud rate: 9600 // actual: baud rate:9600 (0.0%) void uart0_init(void) {  UCSRB = 0x00; //disable while setting baud rate  UCSRA = 0x00;  UCSRC = BIT(URSEL) | 0x06;  //Conf
[Microcontroller]
AVR port set direction first or assign value first?
I have studied this issue carefully, and finally concluded that it is more reasonable to consider the level first and then the direction. As the 4th floor said, it may take a short time, but it still exists. The default value of level and direction register is 0x00. DDRX = 0X00; PROTX = 0X00; If we want to set it to
[Microcontroller]
AVR-SPI bus control
Set to slave mode: void spi_init_s() {    DDRB.6=1;                //PB6 - output    DDRB.4=0;    DDRB.5=0;    DDRB.7=0;            SPCR=0XC0; }   Set to master mode: void spi_init_m() {    DDRB.6=0;    DDRB.4=1;    DDRB.5=1;    DDRB.7=1;              SPCR=0x52; SPSR=0X01; }  
[Microcontroller]
AVR microcontroller proteus simulation lesson 3: single digital tube
Select 7SEG-MPX1-CC for the digital tube, a common cathode digital tube. That is, low level selection (rightmost pin), the 8 pins on the left side of the digital tube, from left to right, from low to high, are connected to PC0---PC7 respectively. The c program is as follows: #include avr/io.h #include util/delay.h
[Microcontroller]
AVR microcontroller proteus simulation lesson 3: single digital tube
Transplantation of MCU 51 to AVR Program
Summarize the conversion from C51 to ICCAVR  1. Header file Replace the header files that define registers in C51, such as reg51.h and at89x51.h, with the corresponding AVR header files, such as io8515.h  and io2313.h. 2. Interrupt handling function In C51, the interrupt keyword is used to indicate that a function
[Microcontroller]
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号