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

📄 test.c

📁 lcd interface code for lpc214x series micro controllers
💻 C
字号:
// Copyright (c) 2001-2003 Rowley Associates Limited.
//
// This file may be distributed under the terms of the License Agreement
// provided with this software.
//
// THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
////////////////////////////////////////////////////////////////////////////////

#include "lpc210x.h"
#include <inttypes.h>
#include <string.h>
#include "lcd.h"
#include "uart.h"
#include "intgrt.h"
#include "i2c.h"	






#define LEDMASK 0x1000
#define RELAYMASK 0x2000

//BUTTONS

#define BUTTON1 0x08000000   //P0.27
#define BUTTON2 0x10000000   //P0.28
#define BUTTON3 0x20000000   //P0.29
#define BUTTON4 0x40000000   //P0.30
#define BUTTON5 0x80000000   //P0.31



#define SECONDS 0x02                          // define Real Time Clock register addresses
#define MINUTE 0x03
#define HOUR 0x04
#define DAY 0x05
#define WEEKDAY 0x06
#define MONTH 0x07
#define YEAR 0x08

#define NOMASK 0		                      // define constants to indicate if a register should be masked				  
#define MASK 1                                // or not after reading

enum weekdays { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
                FRIDAY, SATURDAY };           // define the weekdays

enum months { JANUARY = 1, FEBRUARY, MARCH, APRIL, MAY, JUNE,
              JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER };
                                              // define the months

struct time                                   // define a structure to store
{                                             // date and time in
  unsigned char seconds;
  unsigned char minute;
  unsigned char hour;
  unsigned char day;
  unsigned char weekday;
  unsigned char month;
  unsigned int year;
};

struct time current_time = {0, 56, 9, 13, MONDAY, MARCH, 00};
                                              // declare a structure to hold
											  // the current time

 // function prototypes
unsigned char store_time(struct time *t);
unsigned char read_time(struct time *t);
void display_time(struct time *t);


// function store_time
// stores time and date in the Real Time Clock
// passed is a pointer to a time structure containing the time and date to store
// returned is a 1 for success, a 0 for failiure
unsigned char store_time(struct time *t)
{
  WriteRTC(SECONDS, t->seconds);        // store the seconds in the 'seconds' register
 // if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(MINUTE, t->minute);          // store the minutes in the 'minute' register
 // if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(HOUR, t->hour);              // store the hour in the 'hour' register
 // if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(DAY, t->day);                // store the day in the 'day' register
 // if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(WEEKDAY, t->weekday);        // store the weekday in the 'weekday' register
 // if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(MONTH, t->month);            // store the month in the 'month' register
//  if (i2c_status == I2C_ERROR) return 0;      // check for an error
  WriteRTC(YEAR, t->year);              // store the year in the 'year' register
//  if (i2c_status == I2C_ERROR) return 0;      // check for an error
  return 1;
}

// function read_time
// obtains the current time and date from the Real Time Clock
// passed is the pointer to a time structure to store the date and time in
// returned is a 1 for success, a 0 for failiure
unsigned char read_time(struct time *t)
{
  t->seconds = ReadRTC(SECONDS, MASK);   // read the seconds and mask off unused bits in the 'seconds' register
 // if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->minute = ReadRTC(MINUTE, MASK);     // read the minutes and mask off unused bits in the 'minutes' register
//  if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->hour = ReadRTC(HOUR, MASK);         // read the hour and mask off unused bits in the 'hour' register
//  if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->day = ReadRTC(DAY, MASK);           // read the day and mask off unused bits in the 'day' register
 // if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->weekday = ReadRTC(WEEKDAY, MASK);   // read the weekday and mask off unused bits in the 'weekday' register
//  if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->month = ReadRTC(MONTH, MASK);       // read the month and mask off unused bits in the 'month' register
//  if (i2c_status == I2C_ERROR) return 0;       // check for an error
  t->year = ReadRTC(YEAR, MASK);         // read the year and mask off unused bits in the 'year' register
 // if (i2c_status == I2C_ERROR) return 0;       // check for an error
  return 1;
}

// function display_time
// displays the current date and time via the UART
// passed is the pointer to a time structure holding the date and time to display
// nothing is returned
void display_time(struct time *t)
{  	char disp_lsb;
	char disp_msb;
	char start_disp_posx=0;
	char start_disp_posy=0;
 // printf(t->hour, t->minute, t->seconds, t->day, t->month, t->year);

	disp_lsb=((t->hour) & 0x0f)+0x30;
	disp_msb=(((t->hour) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+1,start_disp_posy);
	lcd_print(&disp_lsb);

	lcd_gotoxy(start_disp_posx+2,start_disp_posy);
	lcd_print(":");


	disp_lsb=((t->minute) & 0x0f)+0x30;
	disp_msb=(((t->minute) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx+3,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+4,start_disp_posy);
	lcd_print(&disp_lsb);

	lcd_gotoxy(start_disp_posx+5,start_disp_posy);
	lcd_print(":"); 	
	
	disp_lsb=((t->seconds) & 0x0f)+0x30;
	disp_msb=(((t->seconds) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx+6,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+7,start_disp_posy);
	lcd_print(&disp_lsb);

}


void display_date(struct time *t)
{  	char disp_lsb;
	char disp_msb;
	char start_disp_posx=9;
	char start_disp_posy=0;
 // printf(t->hour, t->minute, t->seconds, t->day, t->month, t->year);

	disp_lsb=((t->day) & 0x0f)+0x30;
	disp_msb=(((t->day) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+1,start_disp_posy);
	lcd_print(&disp_lsb);

	lcd_gotoxy(start_disp_posx+2,start_disp_posy);
	lcd_print("/");


	disp_lsb=((t->month) & 0x0f)+0x30;
	disp_msb=(((t->month) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx+3,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+4,start_disp_posy);
	lcd_print(&disp_lsb);

	lcd_gotoxy(start_disp_posx+5,start_disp_posy);
	lcd_print("/"); 	
	
	disp_lsb=((t->year) & 0x0f)+0x30;
	disp_msb=(((t->year) & 0xf0) >>4)+0x30;
	lcd_gotoxy(start_disp_posx+6,start_disp_posy);
	lcd_print(&disp_msb);
	lcd_gotoxy(start_disp_posx+7,start_disp_posy);
	lcd_print(&disp_lsb);

}

extern void init_serial_1 (void);            /* Initialize Serial Interface     */
extern int  putchar_1 (int ch);              /* Write character to Serial Port  */
extern int  getchar_1 (void);                /* Read character from Serial Port */

extern void init_serial_0 (void);            /* Initialize Serial Interface     */
extern int  putchar_0 (int ch);              /* Write character to Serial Port  */
extern int  getchar_0 (void);                /* Read character from Serial Port */


void puthex_1 (int hex) {                    /* Write Hex Digit to Serial Port  */
  if (hex > 9) putchar_1('A' + (hex - 10));
  else         putchar_1('0' +  hex);
}

void putstr_1 (char *p) {                    /* Write string */
  while (*p) {
    putchar_1 (*p++);
  }
}



void puthex_0 (int hex) {                    /* Write Hex Digit to Serial Port  */
  if (hex > 9) putchar_0('A' + (hex - 10));
  else         putchar_0('0' +  hex);
}

void putstr_0 (char *p) {                    /* Write string */
  while (*p) {
    putchar_0 (*p++);
  }
}






void udelay(int val){
      int d = val*69;
      while(d--);
}

void init_buttons(){
     PINSEL1 &= (0x007FFFFF); //enable GPIO
     IODIR &= (0x07FFFFFF); //INPUT - 0
}

int button_pressed(){
      unsigned long val;
      val = IOPIN;
      
      if (!(val&BUTTON1)){
            return 1;
      }
       
      if (!(val&BUTTON2)){
            return 2;
      }
       
      if (!(val&BUTTON3)){
            return 3;
      }
       
      if (!(val&BUTTON4)){
            return 4;
      }
      
      if (!(val&BUTTON5)){
            return 5;
      }
      return 0;
}


 
int
main(void)
{
  int i,a, j;
 // unsigned char  rtc_data[6];
  //int counter;
  //power led
  MAMCR = 2;
  PINSEL0  &= ~((LEDMASK<<2)|(LEDMASK<<1));

  IODIR |= LEDMASK;
  IOCLR |= LEDMASK; //ON LED

  IODIR |= RELAYMASK;
  IOSET |= RELAYMASK; //ON RELAY

  lcd_init();

  lcd_cursor_off();
  init_serial_1();
	init_serial_0();                  
				             /* Initialize Serial Interface   */
  putstr_1("Hello");
  putstr_0("Hello");
  InitI2C();   // this function Initialises I2C 

   if (!store_time(&current_time))              // store the current time in the Real Time Clock held in the current_time
                                               // structure. If there is an error display an error message
  {
   	lcd_gotoxy(0,0);
 	lcd_print("Err writing to RTC");
  }

 a=i;
 
  while(1){
    
    if (read_time(&current_time))              // read the current time
      {
	  	display_time(&current_time);             // display the current time if no error
		display_date(&current_time);             // display the current time if no error
	  }
	else
	{
	  	lcd_gotoxy(0,0);
 		lcd_print("Err writing to RTC");
  	}

       if (i=button_pressed()){

	       if(a!=i){
		   a=i;
           lcd_clear();	 udelay(300);
           switch(i){
                case 1:
				//	 lcd_gotoxy(0,0);
				//	 lcd_print("SW1 Pressed");
					 lcd_gotoxy(1,1);
					 lcd_print("SW1 Pressed");
					 lcd_gotoxy(2,2);
					 lcd_print("SW1 Pressed");
					 lcd_gotoxy(3,3);
					 lcd_print("SW1 Pressed");
					 IOSET |= LEDMASK; //OFF LED
					 putstr_1("Hello");
				 	 putstr_0("Hello");
	                  break;
                case 2:
				//	 lcd_gotoxy(0,0);
				//	 lcd_print("SW2 Pressed");
					 lcd_gotoxy(1,1);
					 lcd_print("SW2 Pressed");
					 lcd_gotoxy(2,2);
					 lcd_print("SW2 Pressed");
					 lcd_gotoxy(3,3);
					 lcd_print("SW2 Pressed");
					  IOCLR |= LEDMASK; //ON LED
                     break;

                case 3:
				//	 lcd_gotoxy(0,0);
				//	 lcd_print("SW3 Pressed");
					 lcd_gotoxy(1,1);
					 lcd_print("SW3 Pressed");
					 lcd_gotoxy(2,2);
					 lcd_print("SW3 Pressed");
					 lcd_gotoxy(3,3);
					 lcd_print("SW3 Pressed");
					 IOCLR |= RELAYMASK; //OFF RELAY
                     break;

                case 4:
				//	 lcd_gotoxy(0,0);
				//	 lcd_print("SW4 Pressed");
					 lcd_gotoxy(1,1);
					 lcd_print("SW4 Pressed");
					 lcd_gotoxy(2,2);
					 lcd_print("SW4 Pressed");
					 lcd_gotoxy(3,3);
					 lcd_print("SW4 Pressed");
					 IOSET |= RELAYMASK; //ON RELAY
                     break;

                case 5:
				//	 lcd_gotoxy(0,0);
				//	 lcd_print("SW5 Pressed");
					 lcd_gotoxy(1,1);
					 lcd_print("SW5 Pressed");
					 lcd_gotoxy(2,2);
					 lcd_print("SW5 Pressed");
					 lcd_gotoxy(3,3);
					 lcd_print("SW5 Pressed");
					 IOSET |= LEDMASK; //OFF LED
					 IOCLR |= RELAYMASK; //OFF RELAY
                     break;
                default:
                        break;

           }
		   }
       }else {
	      if(a!=i+6)
		  {
	     lcd_clear();	 udelay(300);
		 a=i+6;	  // dummy
       //  lcd_gotoxy(0,0);
	//	 lcd_print("ARO Equipments");
		 lcd_gotoxy(1,1);
		 lcd_print("805 Ph-V ");
		 lcd_gotoxy(2,2);
		 lcd_print("Udyog Vihar");
		 lcd_gotoxy(3,3);
		 lcd_print("Gurgaon ");
		 }
       }

       udelay(1000);

  }

}
// EOF

⌨️ 快捷键说明

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