📄 full.c
字号:
/****************************************************
Module: full.c
Description: full demo program for EVBA7 eval board
Version Date Initials Description
1.0.5-M 31-Jan-2003 ASH Initial
1.0.7 26-May-2003 ASH Updated comments and changed
baudrate divisor for default
crystal freq of 14.745MHz
*****************************************************/
// LPC2100 definitions
#include "LPC2100_addrs.h"
// stdio for semihosting
#include <stdio.h>
#include <stdlib.h>
//extern char __heap_start;
//extern char __heap_end;
extern int __end;
//int heapspace[100];
//__heap_start = (int)heapspace;
//__heap_end = (int) &(heapspace[100]); //+ sizeof(char);
//__heap_start = __end;
// helper macro to write to a memory mapped register
#define REG(addr) (*(volatile unsigned long *)(addr))
// types
typedef unsigned int _4BYTE;
typedef unsigned short _2BYTE;
typedef unsigned char _BYTE;
unsigned char ucGlobalTemp;
// local functions
void OutputTo7SegDisplay(unsigned char ucHexVal);
void OutputStringTo7SegDisplay(void);
void InitialiseUART0(void);
void InitialiseI2CForXmit(void);
void InitialiseI2CForRec(void);
void InitialiseI2C(void);
void InitialiseLines(void);
void Write32bitDataUART0(_4BYTE value);
void Write4bitDataUART0(_BYTE value);
void WriteStringUART0(const char *str);
unsigned char ReadSW1(void);
__attribute__ ((interrupt("IRQ"))) void ExternalInterrupt();
void RemapVectorsToRAM(void);
// LPC21000 misc uart0 definitions
#define UART0_PCB_PINSEL_CFG (_4BYTE)0x00000005
#define UART0_INT_BIT (_4BYTE)0x0040
#define LCR_DISABLE_LATCH_ACCESS (_4BYTE)0x00000000
#define LCR_ENABLE_LATCH_ACCESS (_4BYTE)0x00000080
#define LCR_DISABLE_BREAK_TRANS (_4BYTE)0x00000000
#define LCR_ODD_PARITY (_4BYTE)0x00000000
#define LCR_ENABLE_PARITY (_4BYTE)0x00000008
#define LCR_1_STOP_BIT (_4BYTE)0x00000000
#define LCR_CHAR_LENGTH_8 (_4BYTE)0x00000003
#define BAUD_19200 12 // 8 for 10MHz, 12 for 14.745MHz
#define LSR_THR_EMPTY (_4BYTE)0x00000020
// macro to write a character to UART0
#define WriteCharUART0(value) {while((REG(UART0_LSR) & LSR_THR_EMPTY) == 0); \
REG(UART0_THR) = value;}
/*
This program demonstrates the functionality of the EVBA7 evaluation board including:
- writing to UART0 (To view data, connect an RS232 cable between EVBA7 UART0 and Serial Port 1
of your PC. Set HyperTerminal to 19.2Kb/s, 8 data bits, Even Parity, 1 Stop Bits, Xon/Xoff flow)
- Semihosting; ensure correct configuration in PathFinder debugger and that the console window is opened
- Installing an EINT0 interrupt handler and responding to interrupts generated by pressing the SW2 switch
- Using LPC2100 I2C interface to write to D1, the 7-seg display
- Using LPC2100 I2C to read SW1, the 4-way dip-switch
NOTE: this example assumes we are running with a 10MHz crystal on the EVBA7 board, see #define BAUD_19200
NOTE: all messages written to UART0 are also echoed to PathFinder's Console Window using semi-hosting
NOTE: Building and running this example in Flash code may cause problems for features such as semi-hosting,
and it is recommended that these be disabled in a Flash build.
*/
// program main
int main()
{
unsigned char i,j;
unsigned long x;
int *p = malloc (10);
//Initialise interrupts
InitialiseI2C();
InitialiseLines();
RemapVectorsToRAM();
// Initialise the UART0 and I2C pins on the LPC2100 device
REG(PCB_PINSEL0) = ( REG(PCB_PINSEL0) & 0xFFFF0000 )| UART0_PCB_PINSEL_CFG|0x50;
InitialiseUART0();
WriteStringUART0("\r\nAshling EVBA7 Philips LPC2100 Evaluation Board Demo Program\r\n");
WriteStringUART0("\r\nThis program will:");
WriteStringUART0("\r\n- write to UART0");
WriteStringUART0("\r\n- write to 7-Segment Display using I2C/SW1 and SW2 interrupts\n");
// disable semihost call for Flash version
//printf("\r\nAshling EVBA7 Philips LPC2100 Evaluation Board Demo Program\r\n");
InitialiseI2CForXmit();
OutputStringTo7SegDisplay();
WriteStringUART0("\r\nPress the USER EINT0 switch to read SW1 and write to 7-segment display\r\n");
// disable semihost call for Flash version
//printf("\r\nPress the USER EINT0 switch to read SW1 and write to 7-segment display\r\n");
while (1);
return 0;
}
// this function initialises the I2C bus for write to 7-Seg LED
void InitialiseI2CForXmit(void)
{
REG(I2C_I2CONCLR) = 0xFF; // Clear all bits
REG(I2C_I2CONSET) = 0x40; // Setup Mode MASTER RECEIVE.
REG(I2C_I2CONSET) = 0x64; // Set enable bit
REG(I2C_I2DAT) = 0x42; // Set Address, Start Bit and ACK bit
REG(I2C_I2CONCLR) = 0x08; // Clear i2c interrupt bit
REG(I2C_I2CONCLR) = 0x20; // Clear Start bit
}
// this function initialises the I2C bus for receive read from SW1
void InitialiseI2CForRec(void)
{
REG(I2C_I2CONCLR) = 0xFF; // Clear all bits
REG(I2C_I2CONSET) = 0x40; // Setup Mode MASTER RECEIVE.
REG(I2C_I2CONSET) = 0x64; // Set enable bit
REG(I2C_I2DAT) = 0x41; // Set Address, Start Bit and ACK bit.
REG(I2C_I2CONCLR) = 0x08; // Clear i2c interrupt bit
REG(I2C_I2CONCLR) = 0x20; // Clear Start bit
}
// read SW1 via I2C
unsigned char ReadSW1(void)
{
unsigned int j,uiHexVal;
// first, read SW1 value in PCF8574T status register
REG(I2C_I2CONCLR) = 0x08;
// Read the i2c ststus register to see if Data has been read correctly
while(REG(I2C_I2STAT) != 0x40)
{
}
// Read the data...
uiHexVal = REG(I2C_I2DAT);
// now read into LPC2100 I2C status register
REG(I2C_I2CONCLR) = 0x08;
// Read the i2c ststus register to see if Data has been read correctly
while(REG(I2C_I2STAT) != 0x50)
{
}
// Read the data...
uiHexVal = (~REG(I2C_I2DAT)) & 0xF ;
return uiHexVal;
}
// this function outputs ucHexVal (<=0xF) to EVBA7 7-segment display via the I2C bus
void OutputTo7SegDisplay(unsigned char ucHexVal)
{
const unsigned char ucSegVals[]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
// assert(ucHexVal <= 0xF);
REG(I2C_I2DAT) = ucSegVals[ucHexVal];
REG(I2C_I2CONCLR) = 0x08;
}
// this function outputs a string to EVBA7 7-segment display via the I2C bus
void OutputStringTo7SegDisplay()
{
int i = 0;
int j = 0;
const unsigned char ucSegVals[]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90, //0,1,2,3,4,5,6,7,8,9,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, //:,;,<,=,>,?,@,
0x88,0x83,0xC6,0xA1,0x86,0x8E,0x90,0x8B,0xCF,0xF1, //a,b,c,d,e,f,g,h,i,j,
0xFF,0xC7,0xFF,0xC8,0xA3,0x8C,0xE8,0xCE,0x92,0xF8, //k,l,m,n,o,p,q,r,s,t,
0xC1,0xC1,0xFF,0xFF,0x99,0x92}; //u,v,w,x,y,z
// we want to output "Philips LPC2100 EVBA7 Board" on the 7-seg display
// 012345678901234567890123456
while(j < 27)
{
//delay
for(i=0;i<100000;i++);
switch (j)
{
case 0: // P
case 9:
REG(I2C_I2DAT) = 0x8c;
break;
case 1: // h
REG(I2C_I2DAT) = 0x8b;
break;
case 2: // i
case 4:
REG(I2C_I2DAT) = 0xcf;
break;
case 3: // l
REG(I2C_I2DAT) = 0xc7;
break;
case 5: // p
REG(I2C_I2DAT) = 0x8c;
break;
case 6: // s
REG(I2C_I2DAT) = 0x92;
break;
case 7: // space
case 15:
case 21:
default:
REG(I2C_I2DAT) = 0xFF;
break;
case 8: // L
REG(I2C_I2DAT) = 0xc7;
break;
case 10: // C
REG(I2C_I2DAT) = 0xc6;
break;
case 11: // 2
REG(I2C_I2DAT) = 0xa4;
break;
case 12: // 1
REG(I2C_I2DAT) = 0xf9;
break;
case 13: // 0
case 14:
REG(I2C_I2DAT) = 0xc0;
break;
case 16: // E
REG(I2C_I2DAT) = 0x86;
break;
case 17: // V
REG(I2C_I2DAT) = 0xc1;
break;
case 18: // B
case 22:
REG(I2C_I2DAT) = 0x83;
break;
case 19: // A
REG(I2C_I2DAT) = 0x88;
break;
case 20: // 7
REG(I2C_I2DAT) = 0xf8;
break;
case 23: // o
REG(I2C_I2DAT) = 0xa3;
break;
case 24: // a
REG(I2C_I2DAT) = 0x88;
break;
case 25: // r
REG(I2C_I2DAT) = 0xce;
break;
case 26: // d
REG(I2C_I2DAT) = 0xa1;
break;
}
REG(I2C_I2CONCLR) = 0x08;
j++;
/*
// the toupper call doesn't work from flash, hard-code the values
*str=toupper(*str);
if((*str>=0x30) && (*str <=0x5A)) //If current character is not a letter clear display
REG(I2C_I2DAT) = ucSegVals[toupper(*str)-0x30];
else
REG(I2C_I2DAT) = 0xFF;
REG(I2C_I2CONCLR) = 0x08;
str++;
*/
}
}
// this function sets up UART0
void InitialiseUART0(void)
{
int i,xflag;
_4BYTE xdata;
// enable access to divisor latch regs
REG(UART0_LCR) = LCR_ENABLE_LATCH_ACCESS;
// set divisor for desired baud
REG(UART0_DLM) = 0;
REG(UART0_DLL) = BAUD_19200;
// disable access to divisor latch regs (enable access to xmit/rcv fifos
// and int enable regs)
REG(UART0_LCR) = LCR_DISABLE_LATCH_ACCESS;
// disable all UART0 interrupts
REG(UART0_IER) = 0;
// setup fifo control reg - trigger level 0 (1 byte fifos), no dma
// disable fifos (450 mode)
REG(UART0_FCR) = 0;
// setup line control reg - disable break transmittion, even parity,
// 1 stop bit, 8 bit chars
REG(UART0_LCR) = 0x1B;
}
void InitialiseLines(void)
{
REG(PCB_PINSEL1) = 0x1;
// Enable bit 14, which is the external interrupt 0...
REG(VICIntEnable) = 0x4000;
// Clear the interrupt status flag...
REG(SYSCON_EXTINT) = 0x1;
}
// write a 32-bit value to UART0
void Write32bitDataUART0(_4BYTE value)
{
int offset;
_BYTE xdata;
for(offset = 28; offset >=0; offset -= 4)
{
xdata = (value >> offset) & 0xF;
xdata = (xdata <= 9)?'0' + xdata : 'A' + (xdata - 10);
WriteCharUART0(xdata);
}
}
// write a 4-bit value to UART0
void Write4bitDataUART0(_BYTE value)
{
_BYTE xdata;
xdata = value & 0xF;
xdata = (xdata <= 9)?'0' + xdata : 'A' + (xdata - 10);
WriteCharUART0(xdata);
}
// write a string to UART0
void WriteStringUART0(const char *str)
{
int i;
i = 0;
while(str[i] != 0)
WriteCharUART0(str[i++]);
}
__attribute__ ((interrupt("IRQ"))) void ExternalInterrupt()
{
unsigned char j;
char ucMsg[0x30] = {0};
int x;
InitialiseI2CForRec();
j=ReadSW1();
ucGlobalTemp=j;
InitialiseI2CForXmit();
// wait until we're ready
while(REG(I2C_I2STAT) != 0x18)
{
}
OutputTo7SegDisplay(ucGlobalTemp);
// print out to serial port.
// WriteCharUART0 is called directly (ie not via WriteStringUART0) as it is an interrupt
// and to avoid corruption of the string
sprintf(ucMsg,"EINT0 occured, SW1 = 0x%x\r\n",ucGlobalTemp);
x = 0;
while(ucMsg[x] != 0)
WriteCharUART0(ucMsg[x++]);
// disable semihosting call
//printf("EINT0 occured, SW1 = 0x%x\n",ucGlobalTemp);
// Before returning, wait for the external interrupt line to got high...
while (REG(SYSCON_EXTINT) & 1)
REG(SYSCON_EXTINT) = 0x1;
}
void RemapVectorsToRAM(void)
{
// set to User flash mode. Interrupt vectors are not re-mapped and reside in flash
REG(SYSCON_MEMMAP) = 0x1;
}
void InitialiseI2C(void)
{
REG(I2C_I2CONCLR) = 0xFF;
// Set pinouts as scl and sda
REG(PCB_PINSEL0) = 0x50;
REG(I2C_I2CONSET) = 0x40;
REG(I2C_I2CONSET) = 0x64;
REG(I2C_I2DAT) = 0x42;
REG(I2C_I2CONCLR) = 0x08;
REG(I2C_I2CONCLR) = 0x20;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -