📄 main.c
字号:
/* *********************************************************
Function declarations
********************************************************* */
void Initialize(void);
void feed(void);
void IRQ_Routine (void) __attribute__ ((interrupt("IRQ")));
void FIQ_Routine (void) __attribute__ ((interrupt("FIQ")));
void SWI_Routine (void) __attribute__ ((interrupt("SWI")));
void UNDEF_Routine (void) __attribute__ ((interrupt("UNDEF")));
/**********************************************************
Header files
**********************************************************/
#include "LPC210x.h"
#include "lcd.h"
//==============================================================================
// Local Prototypes
//==============================================================================
void init_buttons();
void udelay(int val);
int button_pressed();
//==============================================================================
// Local constants
//==============================================================================
#define LEDMASK 0x1000
#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
const unsigned char MyName[]={"Kristian"};
/**********************************************************
MAIN
**********************************************************/
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
main(void)
{
int i, j;
MAMCR = 2;
unsigned char *p = MyName;
//power led
PINSEL0 &= ~((LEDMASK<<2)|(LEDMASK<<1));
IODIR |= LEDMASK;
IOCLR |= LEDMASK; //ON LED
lcd_init();
lcd_cursor_off();
//main program
lcd_print("Kristian");
lcd_cursor_home();
udelay(1000);
/*for (j = 0; j < 10; j++ );
{
lcd_write_nibbles(0x14);
udelay(1000);
for (j = 0; j < 300000; j++ ); // wait 500 msec
}*/
lcd_write_nibbles(0xC0);
udelay(1000);
lcd_print("Dilov");
udelay(1000);
lcd_write_nibbles(0x80);
udelay(1000);
for (j = 0; j < 10; j++ );
{
lcd_write_nibbles(0x14);
udelay(1000);
for (j = 0; j < 300000; j++ ); // wait 500 msec
}
while(1){
int i;
}
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void udelay(int val){
int d = val*69;
while(d--);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void init_buttons(){
PINSEL1 &= (0x007FFFFF); //enable GPIO
IODIR &= (0x07FFFFFF); //INPUT - 0
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
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;
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_wait()
{
int loop=2800; //more than enough
//busy loop
while(loop--);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_out_data4(unsigned char val){
IOCLR |= (LCD_DATA);
IOSET |= (val<<4);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_write_nibbles(unsigned char val){
//higher-order byte
lcd_en_set();
lcd_out_data4((val>>4)&0x0F);
lcd_en_clr();
lcd_wait();
//lower-order byte
lcd_en_set();
lcd_out_data4((val)&0x0F);
lcd_en_clr();
lcd_wait();
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_write_control(unsigned char val){
lcd_rs_clr();
lcd_write_nibbles(val);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_init(){
PINSEL0 &= (~LCD_GPIO_SEL0);
PINSEL1 &= (~LCD_GPIO_SEL1);
/* we only work on OUTPUT so far */
IODIR |= LCD_IOALL;
/* IO init complete, init LCD */
/* init 4-bit ops*/
lcd_rs_clr();
lcd_rw_clr();
lcd_en_clr();
//wait VDD raise > 4.5V
lcd_wait();
//dummy inst
lcd_write_nibbles(0x30);
lcd_write_nibbles(0x30);
lcd_write_nibbles(0x30);
//FUNCTION SET
//001DL N F XX
//DL=1: 8bit
//DL=0: 4bit
//N=0: 1 line display
//N=1: 2 line display
//F=0: 5x7 dots
//F=1: 5x10 dots
//our case:
//0010 1000
lcd_en_set();
lcd_out_data4(0x2);
lcd_en_clr();
lcd_wait();
lcd_write_nibbles(0x28);
//LCD ON
lcd_write_nibbles(0x0E);
//Clear Display
lcd_write_nibbles(0x01);
//Entry mode
lcd_write_nibbles(0x06);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_putchar(unsigned char c){
lcd_rs_set();
lcd_write_nibbles(c);
}
//==============================================================================
// DESCRIPTION :
// PARAMETERS (Type,Name,Min,Max) :
// RETURN VALUE :
// DESIGN INFORMATION :
//==============================================================================
void lcd_print(unsigned char* str){
int i;
//limit 1 line display for prints
for (i=0;i<16 && str[i]!=0;i++){
lcd_putchar(str[i]);
}
}
/**********************************************************
Initialize
**********************************************************/
#define PLOCK 0x400
void Initialize(void) {
// Setting the Phased Lock Loop (PLL)
// ----------------------------------
//
// Olimex LPC-P2106 has a 14.7456 mhz crystal
//
// We'd like the LPC2106 to run at 53.2368 mhz (has to be an even multiple of crystal)
//
// According to the Philips LPC2106 manual: M = cclk / Fosc where: M = PLL multiplier (bits 0-4 of PLLCFG)
// cclk = 53236800 hz
// Fosc = 14745600 hz
//
// Solving: M = 53236800 / 14745600 = 3.6103515625
// M = 4 (round up)
//
// Note: M - 1 must be entered into bits 0-4 of PLLCFG (assign 3 to these bits)
//
//
// The Current Controlled Oscilator (CCO) must operate in the range 156 mhz to 320 mhz
//
// According to the Philips LPC2106 manual: Fcco = cclk * 2 * P where: Fcco = CCO frequency
// cclk = 53236800 hz
// P = PLL divisor (bits 5-6 of PLLCFG)
//
// Solving: Fcco = 53236800 * 2 * P
// P = 2 (trial value)
// Fcco = 53236800 * 2 * 2
// Fcc0 = 212947200 hz (good choice for P since it's within the 156 mhz to 320 mhz range
//
// From Table 19 (page 48) of Philips LPC2106 manual P = 2, PLLCFG bits 5-6 = 1 (assign 1 to these bits)
//
// Finally: PLLCFG = 0 01 00011 = 0x23
//
// Final note: to load PLLCFG register, we must use the 0xAA followed 0x55 write sequence to the PLLFEED register
// this is done in the short function feed() below
//
// Setting Multiplier and Divider values
PLLCFG=0x23;
feed();
// Enabling the PLL */
PLLCON=0x1;
feed();
// Wait for the PLL to lock to set frequency
while(!(PLLSTAT & PLOCK)) ;
// Connect the PLL as the clock source
PLLCON=0x3;
feed();
// Enabling MAM and setting number of clocks used for Flash memory fetch (4 cclks in this case)
MAMCR=0x2;
MAMTIM=0x4;
// Setting peripheral Clock (pclk) to System Clock (cclk)
VPBDIV=0x1;
}
void feed(void)
{
PLLFEED=0xAA;
PLLFEED=0x55;
}
/* Stubs for various interrupts (may be replaced later) */
/* ---------------------------------------------------- */
void IRQ_Routine (void) {
while (1) ;
}
void FIQ_Routine (void) {
while (1) ;
}
void SWI_Routine (void) {
while (1) ;
}
void UNDEF_Routine (void) {
while (1) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -