
//// MikroC for PIC code
float pot;
void main() {
ADC_Init();
Delay_ms(100);
PWM1_Init(5000);
Delay_ms(100);
while(1)
{
PWM1_Start();
pot = ADC_Read(0);
Delay_ms(10);
//// Duty cyle is max at 255
/// While ADC resolution is 10 bit means
//// 2^10 = 1024 or 0 to 1023
//// we need to remap ADC to duty cyle value
pot = pot / 4;
PWM1_Set_Duty(pot);
}
}
MikroC for PIC code files are attached as resources. Please download.
//////////// MikroC for PIC code
int Lpot;
char Lpot1[8];
void main() {
ADC_Init();
Delay_ms(20);
UART1_Init(9600);
Delay_ms(20);
while(1)
{
Lpot = ADC_Read(0);
Delay_ms(20);
IntToStr(Lpot, Lpot1);
Delay_ms(20);
UART1_Write_Text(Lpot1);
UART1_Write_Text("\r\n");
Delay_ms(250);
}
}
////MikroC for PIC code
float raw;
float volt;
char volt1[5];
void main()
{
UART1_Init(9600);
Delay_ms(100);
ADC_Init();
Delay_ms(100);
while(1)
{
raw = ADC_Read(0);
Delay_ms(10);
volt = (raw * 5.0)/1024.0;
Delay_ms(10);
FloatToStr_FixLen(volt, volt1, 8);
Delay_ms(10);
if (UART1_Tx_Idle() == 1)
{
UART1_Write_Text(" Voltage = ");
UART1_Write_Text(volt1);
UART1_Write_Text(" V - ");
Delay_ms(500);
}
}
}
MikroC for PIC code files are attached as resources.
MikroC for PIC code files are attached as resources.
///// MikroC for PIC code
// Lcd pinout settings
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D7 at RD1_bit;
sbit LCD_D6 at RD0_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D4 at RC2_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D7_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC2_bit;
float read1;
float read2;
char read11[5];
void main() {
LCD_Init();
Delay_ms(100);
ADC_Init();
Delay_ms(100);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(100);
while(1) {
read1 = ADC_Read(0);
Delay_ms(100);
read2 = (read1/1024)*100; // (obtained marks / total marks) * 100 = Percentage of your obtained marks
Delay_ms(10);
floattostr(read2,read11);
Delay_ms(10);
LCD_Out(1,1,read11);
LCD_Out(1,6,"% ");
Delay_ms(10);
Delay_ms(200);
}
}
// MikroC for PIC code
// Lcd pinout settings
sbit LCD_RS at RC0_bit;
sbit LCD_EN at RC1_bit;
sbit LCD_D7 at RD1_bit;
sbit LCD_D6 at RD0_bit;
sbit LCD_D5 at RC3_bit;
sbit LCD_D4 at RC2_bit;
// Pin direction
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D7_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC2_bit;
float pot;
char pot1[4];
void main() {
LCD_init();
Delay_ms(1000);
ADC_init();
Delay_ms(1000);
Lcd_Cmd(_LCD_CURSOR_OFF);
Delay_ms(100);
Lcd_Cmd(_LCD_CLEAR);
Delay_ms(100);
while(1)
{
pot = ADC_Read(0);
Delay_ms(100);
if(( potGT0 ) && ( potLT50 ))
{
LCD_OUT(1,1,"0 %");
Delay_ms(100);
}
else if(( po>50 ) && ( pot<100 ))
{
LCD_OUT(1,1,"10 %");
Delay_ms(100);
}
else if(( pot>100 ) && ( pot<200 ))
{
LCD_OUT(1,1,"20 %");
Delay_ms(100);
}
else if(( pot>200 ) && ( pot<300 ))
{
LCD_OUT(1,1,"30 %");
Delay_ms(100);
}
else if(( pot>300 ) && ( pot<400 ))
{
LCD_OUT(1,1,"40 %");
Delay_ms(100);
}
else if(( pot>400 ) && ( pot<500 ))
{
LCD_OUT(1,1,"50 %");
Delay_ms(100);
}
else if(( pot>500 ) && ( pot<600 ))
{
LCD_OUT(1,1,"60 %");
Delay_ms(100);
}
else if(( pot>600 ) && ( pot<700 ))
{
LCD_OUT(1,1,"70 %");
Delay_ms(100);
}
else if(( pot>700 ) && ( pot<800 ))
{
LCD_OUT(1,1,"80 %");
Delay_ms(100);
}
else if(( pot>800 ) && ( pot<900 ))
{
LCD_OUT(1,1,"90 %");
Delay_ms(100);
}
else if( pot>900 )
{
LCD_OUT(1,1,"100%");
Delay_ms(100);
}
}
}
Hello Guys,
Learning how to interface a potentiometer with a PIC16F877A microcontroller is not only about specific to PIC. PIC16F877A is one of the most hardcore microcontroller in the market. If you learn this than you learn all others. Beacuse all have same methodology only compiler environmet differs. For industrial and other commercial or even military projects PIC is one of the most rigid microcontroller.
This course will first take you through a basic introduction to PIC16F877A microcontroller. After that it will lead you towards how to download and install the required softwares. Than we move onto our main topic.
Course outline is as under:
Introduction to PIC16F877A.
Lecture 1: Introduction to PIC16F877A.
Lecture 2: How to go through datasheet of PIC16F877A.
Lecture 3: Pins and ports of PIC16F877A.
Lecture 4: Setting up a PIC16F877A for programming.
Download and install mikroC for PIC and PICKIT 3 programmer tool.
Lecture 5: Download and install MikroC for PIC.
Lecture 6: Download and install PICKIT 3 programmer tool.
PIC16F877A interface Potentiometer tutorials.
Lecture 7: LED brightness control using a potentiometer with a PIC16F877A.
Lecture 8: Display pot value on LM016 LCD in percentage using PIC16F877A.
Lecture 9: Slide potentiometer interface PIC16F877A.
Lecture 10: Using slide potentiometer to control LED brightness using PIC16F877A’s PWM.
Lecture 11: Potentiometer tester using PIC16F877A.
Lecture 12: LED train with speed control using PIC16F877A.
Lecture 13: Control LED blinking speed using a potentiomter with PIC16F877A.
Lecture 14: Use Pot to adjust LED triggering threshold for LDR based light detection.
Lecture 15: DC motor speed control using PWM of PIC16F877A using a POT.
Lecture 16: Display on LCD Pot rotation using PIC16F877A.
Lecture 17: Use if else to use POT values to do different tasks using PIC16F877A.