Function: The timer collects the AD voltage once every 50us, a total of 30 times, and displays it on LCD1602.
Software: MPLAB X IDE v5.35, ISIS 7 Professional
Note: The system clock is configured as an external clock of 8Mhz. Since the data is collected every 50us, the AD collection time should be less than 50us. You can collect AD data at different intervals by adjusting the value of the timer.
code show as below:
#include "pic.h"
#define RS RC1 //command or data
#define RW RC2 //read write io
#define EN RC3 //enable
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_ON & CPD_OFF);
void LCD1602_Init(void);
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str); //display
void Time_Init(void);
void ADC_Init(char k);
int AD_CollectVolt(void);
char AD_Complete_Flag = 0;
unsigned int arr[30];
unsigned int ad_volt = 0;
unsigned long sum = 0;
unsigned char num = 0,j = 0;
unsigned int Data = 0;
unsigned char volt_num[5];
void main(void) //Fosc = 8Mhz
{
PEIE = 1;
GIE = 1;
TRISC = 0X00; //OUTPUT
TRISD = 0X00; //OUTPUT
Time_Init(); //50us interrupt
ADC_Init(0); //Channel 0 RA0
LCD1602_Init();
while(1)
{
AD_CollectVolt();
volt_num[0] = ad_volt/1000 + 48;
volt_num[1] = ad_volt/100%10 + 48;
volt_num[2] = ad_volt/10%10 + 48;
volt_num[3] = ad_volt%10 + 48;
volt_num[4] = '';
LcdShowStr(0, 0, &volt_num);
LcdShowStr(5, 0, "MV");
}
}
void interrupt ISR(void)
{
static unsigned char test = 0;
if((TMR1IE == 1) && (TMR1IF == 1))
{
TMR1IF = 0;
TMR1 = TMR1 + 0xffcd;
if(GO != 1)
{
Data = ADRESH;
Data = Data << 8;
Data = Data | ADRESL;
arr[num]= Data;
num++;
}
GO = 1;
if(test == 0) //test 50us time GPIO
{
RC4 = 0;
test = 1;
}
else
{
RC4 = 1;
test = 0;
}
if(num >= 30)
{
TMR1ON = 0;
TMR1IE = 0;
for(j = 0;j < 30;j ++)
{
sum = sum + arr[j];
}
sum = sum/30;
ad_volt = sum*5000/1024;
sum = 0;
num = 0;
AD_Complete_Flag = 1;
}
}
}
void LCD_WaitReady(void)
{
unsigned char sta;
PORTD = 0xff;
RS = 0;
RW = 1;
do
{
EN = 1;
sta = PORTD;
EN = 0;
}
while(sta&0x80);
}
void Write_Command(unsigned char Command)
{
LCD_WaitReady();
PORTD = Command;
RW = 0;
RS = 0;
EN = 1;
EN = 0;
}
void Write_Data(unsigned char Data)
{
LCD_WaitReady();
PORTD = Data;
RW = 0;
RS = 1;
EN = 1;
EN = 0;
}
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0)
{
addr = 0x00 + x;
}
else
{
addr = 0x40 + x;
}
Write_Command(addr | 0x80);
}
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
LcdSetCursor(x, y);
while (*str != '')
{
Write_Data(*str++);
}
}
void LCD1602_Init(void)
{
Write_Command(0x38);
Write_Command(0x0c);
Write_Command(0x06);
Write_Command(0x01);
}
void Time_Init(void)
{
TMR1CS = 0; //FOSC/4
T1CKPS1 = 0;
T1CKPS0 = 1; //1:2
TMR1 = 0xffcd; //50us
TMR1IF = 0;
TMR1IE = 1;
}
void ADC_Init(char k)
{
TRISA = 0XFF; //input
ADCON1 = 0X80;
ADCON0 = 0b00000001; //FOSC/2
ADCON0 |= (k << 3);
}
int AD_CollectVolt(void)
{
TMR1ON = 1;
TMR1IE = 1;
GO = 1;
do
{
}
while(AD_Complete_Flag == 0); //delay collect volt complete
AD_Complete_Flag = 0;
}
Hardware Diagram:
Connect an external 3V power supply in RA1, and the LCD displays 2998mv;
Note: How to verify whether the data is collected once every 50us? In the timer interrupt function, flip the IO port level and then observe it with an oscilloscope.
As shown in the figure above, the value flips once every 50us, which means that the AD value is collected once every 50us.
Previous article:【MPLAB X IDE】05: Configuration bits
Next article:PIC microcontroller I2C communication (master mode)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- New IsoVu™ Isolated Current Probes: Bringing a New Dimension to Current Measurements
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- Infineon Technologies Launches ModusToolbox™ Motor Kit to Simplify Motor Control Development
- STMicroelectronics IO-Link Actuator Board Brings Turnkey Reference Design to Industrial Monitoring and Equipment Manufacturers
- Melexis uses coreless technology to reduce the size of current sensing devices
- Melexis uses coreless technology to reduce the size of current sensing devices
- Vicor high-performance power modules enable the development of low-altitude avionics and EVTOL
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Chuangshi Technology's first appearance at electronica 2024: accelerating the overseas expansion of domestic distributors
- Recruiting embedded driver engineers
- Negative feedback improves the performance of amplifier circuits
- AD Sampling Notes
- Paid answers to questions about lm5117
- Who is the great god? Who is the great god? What is good for him?
- Altium Designer batch or global modification version
- EEWORLD University ---- Open source H.265 IP core
- Design and application of modular inverter power supply
- Please ask the senior teacher, the principle of the car steering wheel control decoder
- Ask a question