PIC16F884 PIC microcontroller

Publisher:BlissfulMomentsLatest update time:2018-10-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Buck Circuit


PIC16F884 PIC microcontroller 


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.


Reference address:PIC16F884 PIC microcontroller

Previous article:PIC microcontroller drives LCD screen code generation tool
Next article:PIC18F26K80 interrupt priority diagram

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号