Buck Circuit
This is a step-down circuit. SW is connected to PWM here to perform the main work of stepping down. After Vout is divided, the maximum value not exceeding the chip working voltage (5V is also OK) is input to the PIC AN1 port for AD conversion.
The conversion result is displayed on 1602. If the voltage is not the desired voltage, the program changes the PWM duty cycle to achieve the required value.
The main steps are to initialize the 1602 to write data and write instructions and the PIC AD to start the working module. The program is as follows
#include
__CONFIG(0x201A);
#define uint unsigned int
#define uchar unsigned char
#define rs RC4 //Data command
#define en RC5 // Enable terminal
#define DATA PORTD //Data port
//RC1 as PWM output port
const uchar dis1[]="The voltage is:"; //asm("nop");
const uchar dis2[]=" . V";
void delay(uchar time) //1ms
{
uint j;
for(time;time>0;time--)
for(j=160;j>0;j--);
}
void lcd_wcmd(uchar cmd)
{
rs = 0;
en = 0;
DATA = cmd; // pass 8-bit data to 1602 through P0 port
delay(2);
en = 1;
delay(2);
en = 0;
}
void lcd_wdat(uchar dat)
{
rs = 1;
en = 0;
DATA= dat; //Transmit 8-bit data to 1602 through P0 port
delay(2);
en = 1;
delay(2);
en = 0;
}
void lcd_init()
{
TRISC=0Xcf; //1100 1111
TRISD=0X00;
lcd_wcmd(0x38); ////16*2 display, 5*7 dot matrix, 8-bit data
lcd_wcmd(0x0c); //Display on and off cursor
lcd_wcmd(0x06); //Move cursor
lcd_wcmd(0x01); //Clear the LCD display content
}
void pwm_init()
{
TRISC1=1; //CCP2 OUTPUT DISABLE
PR2=0XFF;
CCP2CON=0X3C; //0011(last two digits of CCPR1L))1100 11XX PWM MODE
CCPR2L=0X3F; //CCPR2L:CCP2CON
//0X3F 25%
TMR2IF=0;
T2CON=0X05; //Start timer 2 prescale ratio is 1:4
while(TMR2IF)
{
TMR2IF=0;
TRISC1=0;
}
}
void adc_init()
{
ADCON0=0X45; // f/2 frequency AN1 channel AD conversion completed. Disable ADC
ADCON1=0X80; //Right-aligned reference voltage VSS VDD
TRISA=0X01; //Input
delay(5);
}
void deal_ad()
{
uint temp;
uint ad_sample;
uint result;
uchar a1,a2,a3;
GO_DONE=1;
while(GO_DONE);
ad_sample=ADRESL (ADRESH<<8);
result=(ad_sample*500)>>10; //reference voltage 5V is enlarged 100 times
temp=result;
a1=temp/100;
a2=temp0/10;
a3=temp0;
if(a1!=0)
{
lcd_wcmd(0x80 0x44);
lcd_wdat(a1 0x30);
}
lcd_wcmd(0x80 0x45);
lcd_wdat(a2 0x30);
lcd_wcmd(0x80 0x47);
lcd_wdat(a3 0x30);
}
void lcd_dis()
{
uchar i;
lcd_wcmd(0x80);
i=0; //Set the display position to the 5th character in the first line
while(dis1[i]!='\0')
{ //Display the characters "welcome!"
lcd_wdat(dis1[i]);
i ;
}
lcd_wcmd(0x80 0X40);
i=0; //Set the display position to the 5th character in the first line
while(dis2[i]!='\0')
{ //Display the characters "welcome!"
lcd_wdat(dis2[i]);
i ;
}
}
void main()
{
delay(10);
lcd_init(); //Initialize LCD Just write the starting address
adc_init();
while(1)
{
lcd_dis();
deal_ad();
}
// pwm_init();
while(1);
}
It's not perfect, just for reference. The PWM adjustment is not perfect, but it's not very difficult.
Previous article:PIC microcontroller drives LCD screen code generation tool
Next article:PIC18F26K80 interrupt priority diagram
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- 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
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- 【phyBOARD-i.MX 8M Plus Development Board】Part 2: Powering on the Development Board and Evaluating the Development Environment
- 22 Ways to Prevent EMI When Designing Power Supplies
- 【NXP Rapid IoT Review】Hello Touch
- RISC-V MCU Application Development Series Tutorial CH32V103
- Communication between Yitong Chuanglian MODBUS to PROFIBUS gateway and Honeywell DCS system
- Let's take a look
- 【TI Wireless】Micro Dual-Mode Wireless Receiver
- PWM and PFM
- Let’s talk about whether Huawei can survive in the end.
- Can the network cable be directly soldered to the PCB without a crystal plug or can the network cable be plugged into the circuit board with terminals?