PIC16F917 Voltmeter

Publisher:ww313618Latest update time:2016-09-05 Source: eefocusKeywords:PIC16F917 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
After debugging the NOKIA5110 LCD program in the afternoon, I practiced the PIC16F917 internal AD program and displayed the data collected by AD through LCD. Make a simple voltmeter.
PIC16F917 Voltmeter - Xiaowen - Xiaowen Electronic Design

 /*************PIC16F917 MCU program******************************/
/************************************************************************/
/*****File Function : AD acquisition, LCD display program *****/
/*****Program Author : ZhengWen(ClimberWin) *****/
/*****MCU : PIC16F917 internal crystal oscillator *****/
/*****Compile Date : 2010/08/18 *****/
/*****Edition Info : V1.0 *****/
/*********************************************************************/
//Measure AD and display it through LCD
//Pin definition: PORTD=8LED, KEY=RB0(INT) RA5(AN4) as AD input
/*Modification Date: */

/************************************/
#include
#include "english_6x8_pixel.h"
#define uchar unsigned char
#define uint unsigned int
void Init(void);     //初始化子程序
void LCD_init(void); //LCD初始化程序
void LCD_clear(void);
void LCD_write_char(unsigned char c);
void LCD_set_XY(unsigned char X, unsigned char Y);
void LCD_write_english_string(unsigned char X,unsigned char Y,const char *s);
void LCD_write_byte(unsigned char data, unsigned char command);
void delayms(unsigned int count);
void  interrupt  ADint(void);

#define KEY       RB0
#define SPI_CLK                RD0  
#define SPI_MOSI               RD1 
#define LCD_DC                 RD2 
#define LCD_CE                 RD3
#define LCD_RST                RD4 
/***********************************************/

const unsigned char mask_table[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
float ad_data;//AD数据存储地址
 uchar temp1,temp2;
uint temp3;
/*********************************************/  
void delayms(unsigned int count)
{
 uint i,j;
 for(i=0;i  for(j=0;j<120;j++);
}
/*********************************************/  
void Init(void)
 {
     PORTA = 0B00000000;
     PORTB = 0B00000000;
     PORTD = 0B00000000;    

     TRISA = 0B00100010; //Set RA5 (AN4) RA1 as input
     TRISB = 0B00100001; //Set RB0 as input, as a key port
     TRISD = 0B00000000; //Set PORTD as output, as LCD/LED display port
     RD5=1; //Turn off LED
     RD6=1;
     RD7=1;

///////////AD configuration////////////////////////////////////////
     ANSEL= 0B00010000; //Select AN4 as AD input (PDF 148)
     ADCON0=0B10010001; //AD result format Right justified, select reference voltage VDD-VSS, AN4 input, turn off AD conversion
     ADCON1=0B01010000; //AD conversion clock selection


     ADIE=1;//AD interrupt enable
     PEIE=1;
     ADIF=0;//clear interrupt flag
     GIE=1; //enable general interrupt
/////////////////////////////////////
     LCD_init(); //initialize LCD 
}

void interrupt ADint(void)
  { 
    
    temp1=ADRESL;
    temp2=ADRESH;
    temp3=temp2*256+temp1; //Dx value
    ad_data=(temp3*33000)/1024; //voltage value
    ADIF=0; //clear interrupt flag
    
  }


void LCD_init(void)
  {
    LCD_RST=0; //LCD reset
    NOP();
    LCD_RST=1;
    
    LCD_CE=0 ; //Disable LCD
    NOP();
    LCD_CE=1; //Enable LCD
    NOP();

    LCD_write_byte(0x21, 0); // Use extended commands to set LCD mode
    LCD_write_byte(0xc8, 0); // Set bias voltage
    LCD_write_byte(0x06, 0); // Temperature correction
    LCD_write_byte(0x13, 0); // 1:48
    LCD_write_byte(0x20, 0); // Use basic commands
    LCD_clear(); // Clear the screen
    LCD_write_byte(0x0c, 0); // Set display mode, normal display
        
    LCD_CE=0 ; // Turn off LCD
  }

/////////LCD清屏程序/////////////
void LCD_clear(void)
{
    uint i;
    LCD_write_byte(0x0c, 0);   
    LCD_write_byte(0x80, 0);   
    for (i=0; i<504; i++)
    LCD_write_byte(0x00, 1);//清零  
}

///////////设置LCD坐标///////////////////
void LCD_set_XY(unsigned char X, unsigned char Y)
{
    LCD_write_byte(0x40 | Y, 0);   
    LCD_write_byte(0x80 | X, 0);      
}

/////////////////Character display program/////////////////////
void LCD_write_char(unsigned char c)
{
    uint line;
    c=c-32;
    for (line=0; line<6; line++)
    LCD_write_byte( font6x8[c][line], 1);
}


/////////////////打印字符串/////////////////////////
void LCD_write_english_string(unsigned char X,unsigned char Y, const unsigned char *s)
  {
    uchar i = 0;
    LCD_set_XY(X,Y);
    while(*s) {LCD_write_char(*s++);} 
  }


////////////Write data to LCD//////////////////////
void LCD_write_byte(unsigned char data, unsigned char command)
{
uchar i;
    LCD_CE=0 ; // Enable LCD
    
    if (command == 0)
      {LCD_DC=0 ;} // Send commandelse
    {
      LCD_DC=1 ;} // Send datafor
 
(i=0;i<8;i++)
{
if(data&mask_table[i])
{SPI_MOSI=1;}
else
{SPI_MOSI=0;}
SPI_CLK=0;
NOP();
SPI_CLK=1;
}

    LCD_CE=1; // Turn off LCD
}

 


/////////////Main program///////////////////////////
void main (void)
{

 Init();//初始化程序 
 LCD_clear();   //LCD清屏
 delayms(1000);
 LCD_write_english_string(0,0,"   AD Test   " ); 
 LCD_write_english_string(0,1,"AD4 Vref=3.3V" ); 

 ADCON0=ADCON0|0B00000010;//开始AD转换
     LCD_set_XY(0,3);
    LCD_write_char('D');
    LCD_write_char('x');
    LCD_write_char(' ');
    LCD_write_char('=');

 
 LCD_set_XY(0,5);
    LCD_write_char('V');
    LCD_write_char('i');
    LCD_write_char('n');
    LCD_write_char('='); 
 LCD_set_XY(72,5);
    LCD_write_char('V');
  while(1)
  {
  LCD_set_XY(30,3);
  LCD_write_char((temp3/1000)+16+32);
 LCD_write_char( (temp3%1000)/100+16+32);
 LCD_write_char( ((temp3%1000)%100)/10+16+32);
 LCD_write_char( ((temp3%1000)%100)%10+16+32);

//ad_data=ad_data*10000;
temp3=(uint)ad_data;
LCD_set_XY(30,5);
 LCD_write_char((temp3/10000)+16+32);
LCD_write_char('.');
 LCD_write_char( (temp3%10000)/1000+16+32);
 LCD_write_char( ((temp3%10000)%1000)/100+16+32);
LCD_write_char( (((temp3%10000)%1000)%100)/10+16+32);
LCD_write_char( (((temp3%10000)%1000)%100)%10+16+32);

delayms(100);
 ADCON0=ADCON0|0B00000010; //Start AD conversion
 
  }
 

}

Keywords:PIC16F917 Reference address:PIC16F917 Voltmeter

Previous article:PIC16F917 NOKIA5110 LCD arbitrary dot drawing program
Next article:PIC16F917 NOKIA5110 LCD test program

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号