⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 LCD using 4 bit mode on a s08 (make sure to adjust delay for faster clocks, ie make delay longer)
💻 C
字号:
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <stdio.h>

/* Define a shortname for variable types */ 
typedef unsigned char				UINT8;  /*  8 bits */
typedef unsigned short int	UINT16; /* 16 bits */
typedef unsigned long int		UINT32; /* 32 bits */

typedef signed char					INT8;   /*  8 bits */
typedef signed short int		INT16;  /* 16 bits */
typedef signed long int			INT32;  /* 32 bits */

typedef volatile UINT8			VUINT8;  /*  8 bits */
typedef volatile UINT16			VUINT16; /* 16 bits */
typedef volatile UINT32			VUINT32; /* 32 bits */

//Register select line
#define	LCD_RSH	(PTDD_PTDD4= 1);
#define	LCD_RSL	(PTDD_PTDD4= 0);
//Read/Write
#define	LCD_RWH	(PTDD_PTDD5= 1);
#define	LCD_RWL	(PTDD_PTDD5= 0);
//Enable line
#define	LCD_EH	(PTDD_PTDD6 = 1);
#define	LCD_EL	(PTDD_PTDD6 = 0);
#define	LCD_STROBE_E	((PTDD_PTDD6 = 1),(delay(0xFF)),(PTDD_PTDD6=0));

UINT16 ones, tenths, hundredths;
UINT16 adc_val;
UINT32 temp;
char volts[10];

void initSystems(void), InitADC(void), SetADC(byte, byte);
void initLCD(void), LCDCmd(UINT8), LCDChar(UINT8), dsplyString(UINT8 *);
void delay(UINT32);

void main(void) {
  
  initSystems();
  InitADC();
  initLCD();

  SetADC(0x0A,0x01);                      // ADC10, enable interrupt
  EnableInterrupts;                       /* enable interrupts */
   
  LCDCmd(0x0C);  
    
    while(1){

      temp = (ulong)adc_val * 7662; //7324 for 3V
      
      ones = temp / 10000000;
      temp = (temp - (10000000* ones));
      tenths = temp/1000000;
      temp = (temp - (1000000*tenths));
      hundredths = temp /100000;

      sprintf(volts, "gm:%d.%d%d",  ones, tenths, hundredths);
      dsplyString(volts);
      LCDCmd(0x02);
      delay(0xFF);
    } 
}
/************************** System Initializations ***************************/
void initSystems() {
  
  SOPT1  = 0x03;                          // Disable COP,RSTO, enable BKGD,RESET
  SOPT2  = 0x00;                          // 
  SPMSC1 = 0x00;                          // Disable LVD
  SPMSC2 = 0x00;                          // Disable power-down modes
  SPMSC3 = 0x00;                          // Disable LVWIE, low trip points
  SCGC1  = 0xFF;                          // Enable bus clock to ADC 
  SCGC2  = 0xFF;                          // 
  
  ICSC1 = 0b00000100;   /* Output of FLL is selected and Internal Reference Selected */
  ICSC2 = 0b00000000;   /* Bus frequency divided by 1 */
  ICSTRM = *(unsigned char*far)0xFFAF; /* Initialize ICSTRM register from a non volatile memory */
  ICSSC = (*(unsigned char*far)0xFFAE) | 0xA0;  /* Initialize ICSSC register from a non volatile memory */
                                                /* High range selected and Maximum frequency enabled */    
} 

/*************************** ADC Initialization *******************************/
void InitADC(void) {
  
  ADCCFG = 0x17;
  ADCSC2 = 0x00;
  ADCSC1 = 0x00;
  
  ADCSC1_ADCO = 1;      //hardware trigger interrupt
  
  APCTL2 = 0x04; 
} 
/**************************  SetADC to pick ADC Channel  **********************/
void SetADC(byte adc_channel, byte aien_value) {

  ADCSC1_AIEN = aien_value&0x01;
  ADCSC1_ADCH = adc_channel;
}

/***************************  ADC Interrupt  ****************************/
interrupt VectorNumber_Vadc void   ADC_ISR(void) {

  if(ADCSC1_COCO ==1)
    adc_val = ADCR;

}

/***************************  LCD Connections  ****************************
4-bit mode

PTD0-PTD3 = DB4-DB7   Data bus
PTD4      = RS        Register select
PTD5      = R/W       Read / Write
PTD6      = E         Enable

PORTD 0b00000000
         |||||||
         |||---- data bus  0x0F
         |||---- RS        0x10
         ||----- R/W       0x20
         |------ Enable    0x40  
*/        

/***************************  Initialization of LCD  ************************/
void initLCD(void) { 

    PTDDD = 0xFF;                       //Set PortC as output
    PTDD = 0x00;                        //Set PortC outputs to 0x00
    LCD_RWL;                            //Make sure it's in Write mode
    
    PTDD=0x03;  LCD_STROBE_E;   delay(0xFFFF);
    
    PTDD=0x03;  LCD_STROBE_E;   delay(0xFF);    
    
    PTDD=0x03;  LCD_STROBE_E;   delay(0xFF);
    
    PTDD=0x02;  LCD_STROBE_E;   delay(0xFF);
        
    LCDCmd(0x28);
    LCDCmd(0x08);
    LCDCmd(0x01);
    LCDCmd(0x06);
    LCDCmd(0x0F);
}

/****************************  LCD Commands  ****************************/
/*
0x01;	clears screen
0x38;	sets up the display initalization
0x0F;	turns display on
0x06;	sets the cursor on and blinking
0x02;	move cursor to start and don't destroy data
0x18;	shifts cursor to the left when a char is entered
0x1C;	shifts cursor to the right when a char is entered
0x10;	moves 1 char to the left
0x14; moves 1 char to the right
0x80;	add this to the address of the display to set the cursor position wanted
*/

void LCDCmd(UINT8 ch) {

    UINT8 ln, un;    //lower and upper nibble
    ln = ch & 0x0F;
    un = ch >>4;

    delay(0xFF);
    PTDD = un;    LCD_STROBE_E; 
      
    delay(0xFF);  
    PTDD = ln;    LCD_STROBE_E;
    
}

/**********************  Places a Char on LCD screen  ***********************/
void LCDChar(UINT8 ch) {

    UINT8 ln, un;    //lower and upper nibble
    ln = ch & 0x0F;
    un = ch >>4;
    
    //upper nibble first
    PTDD = un;    LCD_RSH;  LCD_STROBE_E
        
    //lower nibble
    PTDD = ln;    LCD_RSH;  LCD_STROBE_E;    
        
    LCD_RSL;
}

/************************  Places a String on LCD screen  ********************/
void dsplyString(UINT8 *msgptr) {
  while(*msgptr){
    LCDChar( *msgptr);
    msgptr++;
  }
}

/*************************   delay code  ***************************/
void delay(UINT32 cnt)
{ int i;
  for(i=0;i<cnt;i++){
  }
}  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -