PIC microcontroller ad sampling and display on LCD

Publisher:逍遥游侠Latest update time:2019-11-09 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere


/* Main.c file generated by New Project wizard

*

* Created: Tuesday April 10, 2018

* Processor: PIC18F452

* Compiler: MPLAB C18

*/

#include

#define RW PORTBbits.RB5

#define RS PORTBbits.RB6

#define EN PORTBbits.RB7

#define P0 PORTD

#define ADGO ADCON0bits.GO

void tmr(void);

void KeyInterrupt1 (void); //Interrupt service function

void KeyInterrupt2 (void); //Interrupt service function

void KeyInterrupt3 (void); //Interrupt service function

void lcd_show(void);

void lcd_show1(void);

void lcd_show2(void);

void lcd_show3(void);

void lcd_show4(void);

void lcd_show5(void);

void lcd_show6(void);

unsigned int AD_Trs(void);

unsigned int AD_Trs1(void);

unsigned int AD_Trs2(void);

//int j=0;

//int time=0;

//int timem=0;

int intermode=0;

unsigned int lednum=0;

int  max=4000,max1=3900,max2=3800,max3=3700;

int   min=1000,min1=900,min2=800,min3=1000;

unsigned char time2[7]="wangkuod";

unsigned int a1=0,a2=0,a3=0;

    int init(); //Declare initialization function

     int write_com(unsigned char); //declare write command function

      int write_date(unsigned char); //declare the write data function

      int delay(unsigned char); //declare delay function

      unsigned char x;

//sbit RS = P1^0;

  // sbit RW = P1^1;

  //sbit EN = P1^2;


//unsigned char table[]="HELLO!";

//unsigned char table1[]="2018/4/16";

void main(void) //Main function

{    

    TRISD=0;

TRISBbits.TRISB0=1;

TRISBbits.TRISB1=1;

TRISBbits.TRISB2=1;

    TRISBbits.TRISB5=0;

TRISBbits.TRISB6=0;

TRISBbits.TRISB7=0;

TRISAbits.TRISA0=1;

  TRISAbits.TRISA1=1; 

TRISAbits.TRISA2=1;

TRISAbits.TRISA6=0;


// INTCON |= 0xa0; // Enable global interrupt and Timer0 interrupt

//INTCONbits.TMR0IE=1;

INTCONbits.GIE=1;

//INT0

INTCON2bits.INTEDG0=1;

INTCONbits.INT0IF=0;

INTCONbits.INT0IE=1;

//INT1

INTCON2bits.INTEDG1=1;

INTCON3bits.INT1IF=0;

INTCON3bits.INT1IE=1;

//int2

INTCON2bits.INTEDG2=1;

INTCON3bits.INT2IF=0;

INTCON3bits.INT2IE=1;


//TMR0H = 25536/256; //high 8 bits

//TMR0L = 25536%256; //low 8 bits

//T0CON = 0x88; //Enable Timer0 and set it as a 16-bit timer without prescaler

//AD

ADCON0=0x81;

//-- ADFM ADCS2 —— —— PCFG3 PCFG2 PCFG1 PCFG0

//-- 1 0 0 0 1 1 1 0

//Right-aligned (the upper 6 bits of the ADRESH register are read as 0), AN0 is the analog input bit, reference positive and negative voltage

//VDD and VSS respectively

ADCON1 = 0x8e;


     heat();

  


   // write_com(0x80);    

//    for(x=0;x<6;x++)

//    {

//          write_date(table[x]);

//         delay(150);

//   }


//for(x=0;x<6;x++)

   //  {

   //       write_date(time);

//         delay(150);

//    } 




   //  write_com(0x80);    

  //   for(x=0;x<17;x++)

  //  {

//        write_date(table1[x]);

//        delay(150);

//   }

     while(1)

  {

   unsigned int a; 

   lednum = AD_Trs();

   write_com(0x80);

   time2[1]='0';

   lcd_show();

   

   lednum = AD_Trs1();

   write_com(0x80+0x08);

   time2[1]='1';

   lcd_show();

   

   lednum = AD_Trs2();

   write_com(0x80+0x40);

   time2[1]='2';

   lcd_show();

  switch(intermode) {

  case 0: {write_com(0x80+0x40+0x08);

  lcd_show1();break;

   }

      

case 1:

   {write_com(0x80+0x40+0x08);

   lcd_show2();break;}

   

  case 2: 

   {write_com(0x80+0x40+0x08);

   lcd_show3();break;

   }

   

  case 3: 

   {write_com(0x80+0x40+0x08);

   lcd_show4();break;

   }

  

  case 4: 

   {write_com(0x80+0x40+0x08);

   lcd_show5();break;

   }


  case 5: 

   {write_com(0x80+0x40+0x08);

   lcd_show6();break;

   }

   

default:

  {write_com(0x80);

   lcd_show1();break;

   }

  }

   

if(lednum>max1|lednum>max2|lednum>max3|lednum//for(a=10;a>0;a--)

//{PORTAbits.RA3=0; 

//   delay(50);

  // PORTAbits.RA3=1;

  //  delay(50);

   // PORTAbits.RA3=0;  

   // delay(50);

   // PORTAbits.RA3=1;  

  //  delay(50);} 

PORTAbits.RA6=1; }

  

  } 

   

}




#pragma code xx=0x0008 //MCC18 interrupt must be implemented with an assembly line

void inter(void) //xx is the priority. If there is no priority, xx can be omitted.

{

if(INTCONbits.INT0IF){

  _asm  

  goto KeyInterrupt1

  _endasm}

else if(INTCON3bits.INT1IF){

   _asm  

   goto KeyInterrupt2

   _endasm

  }

else {

   _asm  

   goto KeyInterrupt3

   _endasm

  }

  

}


//=======Interrupt service function==========

#pragma interrupt KeyInterrupt1

void KeyInterrupt1 (void)

{

if (INTCONbits.INT0IF && INTCONbits.INT0IE) //Interrupt flag bit judgment

{

//T0CONbits.TMR0ON=~T0CONbits.TMR0ON;

INTCONbits.INT0IF = 0; // Clear the interrupt flag

switch(intermode) {

  case 0: 

   {max1=max1+100; break;

   }

      

case 1:

   {max2=max2+100;break;}

   

  case 2: 

   {max3=max2+100;break;

   }

   

  case 3: 

   {min1=min1+100; break;

   }

  

  case 4: 

   {min2=min2+100;break;

   }


  case 5: 

   {min3=min3+100;break;

   }

   

default:

  {max1=max1+100;break;

   }

  } 


}

}

#pragma code

//=======Interrupt service function==========

#pragma interrupt KeyInterrupt2

void KeyInterrupt2 (void)

{

if (INTCON3bits.INT1IF && INTCON3bits.INT1IE) //Interrupt flag bit judgment

{

//T0CONbits.TMR0ON=~T0CONbits.TMR0ON;

//intermode=~intermode;

INTCON3bits.INT1IF = 0; // Clear the interrupt flag

intermode++;

if(intermode>5)

{intermode=0;}

}

}

#pragma code

#pragma interrupt KeyInterrupt3

void KeyInterrupt3 (void)

{

if (INTCON3bits.INT2IF && INTCON3bits.INT2IE) //Interrupt flag bit judgment

{

//time=timem=j=0;

// T0CONbits.TMR0ON=0;


INTCON3bits.INT2IF = 0; // Clear the interrupt flag

switch(intermode) {

  case 0: 

   {max1=max1-100; break;

   }

      

case 1:

   {max2=max2-100;break;}

   

  case 2: 

   {max3=max2-100;break;

   }

   

  case 3: 

   {min1=min1-100; break;

   }

  

  case 4: 

   {min2=min2-100;break;

   }


  case 5: 

   {min3=min3-100;break;

   }

   

default:

  {max1=max1-100;break;

   }

  } 


}

}

#pragma code


int init() // Initialization function body

{

    EN = 0;

     write_com(0X38); //Set 16*2 display, 5*7 dot matrix, 8-bit data interface

     write_com(0X0C); //Set to display, do not display cursor

     write_com(0X06); //When writing a character, the entire screen moves right

     write_com(0X01); //display clear

    return 0;

}

int write_com(unsigned char com) // write command function body

{

    RS = 0;

    RW = 0;

    P0 = com;

     delay(5);

     EN = 1;

     delay(5);

     EN = 0;

    return 0;        

}


int write_date(unsigned char date) // Function body for writing data

{

     RS = 1;

     RW = 0;

     P0 = date;

     delay(5);

     EN = 1;

     delay(5);

     EN = 0;

     return 0;        

}


int delay(unsigned char xms)

{

     unsigned char x,y;

     for(x=xms;x>0;x--)

         for(y=110;y>0;y--);

     return 0;

}

void lcd_show1(void)

{

// unsigned char a,b;

//  unsigned char time2[7];

//   unsigned int time1[2];

//   unsigned int time3[2];

   

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = max1/1000;

  a2 = max1%1000/100;

  a3 = max1%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='0';

   time2[2]='M';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

       

     for(x=0;x<7;x++)

    {

          write_date(time2[x]);

        delay(15);

    }

   

   

   

  

  

  }

  

  

  

  void lcd_show2(void)

{

// unsigned char a,b;

   

//   unsigned int time1[2];

//   unsigned int time3[2];

//  unsigned int a1=0,a2=0,a3=0;

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = max2/1000;

  a2 = max2%1000/100;

  a3 = max2%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='1';

   time2[2]='M';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

       

     for(x=0;x<7;x++)

    {

          write_date(time2[x]);

        delay(15);

    }

   

   

   

  

  

  }

  

void lcd_show3(void)

{

// unsigned char a,b;

//  unsigned char time2[7];

//   unsigned int time1[2];

//   unsigned int time3[2];

//   unsigned int a1=0,a2=0,a3=0;

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = max3/1000;

  a2 = max3%1000/100;

  a3 = max3%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='2';

   time2[2]='M';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

       

     for(x=0;x<7;x++)

    {

          write_date(time2[x]);

        delay(15);

    }

   

   

   

  

  

  }

  

  void lcd_show4(void)

{

// unsigned char a,b;

//   unsigned char time2[7];

//   unsigned int time1[2];

//   unsigned int time3[2];

//   unsigned int a1=0,a2=0,a3=0;

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = min1/1000;

  a2 = min1%1000/100;

  a3 = min1%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='0';

   time2[2]='m';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

       

     for(x=0;x<7;x++)

    {

          write_date(time2[x]);

        delay(15);

    }

   

   

   

  

  

  }

  

  

  void lcd_show5(void)

{

// unsigned char a,b;

//   unsigned char time2[7];

//   unsigned int time1[2];

//   unsigned int time3[2];

//   unsigned int a1=0,a2=0,a3=0;

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = min2/1000;

  a2 = min2%1000/100;

  a3 = min2%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='1';

   time2[2]='m';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

       

     for(x=0;x<7;x++)

    {

          write_date(time2[x]);

        delay(15);

    }

   

   

   

  

  

  }

  

  void lcd_show6(void)

{

// unsigned char a,b;

//   unsigned char time2[7];

//   unsigned int time1[2];

//   unsigned int time3[2];

//   unsigned int a1=0,a2=0,a3=0;

  

  //time3[0]=j/10;

   //time3[1]=j%10;

   

   //time1[0]=time/10;

   //time1[1]=time%10;

  

  

  //This algorithm is used to extract the individual bits of an integer to display on the LCD

  a1 = min3/1000;

  a2 = min3%1000/100;

  a3 = min3%100/10;

  //a4 = lednum%10;

   

   time2[0]='A';

   time2[1]='2';

   time2[2]='m';

   time2[3]='0'+a1;

   time2[4]='.';

    time2[5]='0'+a2;

   time2[6]='0'+a3;

[1] [2]
Reference address:PIC microcontroller ad sampling and display on LCD

Previous article:PIC microcontroller drives LCD12864 to display characters source code
Next article:Proteus simulation and source program of laser communication based on PPM modulation of PIC16F877A 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号