📄 board.c
字号:
#include "board.h"
#define PLOCK 0x400
void SPIWait(void){
//unsigned char dummy;
while((SSPSR & 0x10) );
// dummy=SSPDR;
}
unsigned char SPIGetChar(void){
SSPDR =0xFF;
while((SSPSR & 0x10) );
/* wait for the byte to transfer */
return SPI_RESULT_BYTE; /* Return the received byte */
}
void SPIPutChar(unsigned char c){
while(!(SSPSR & 0x02) );
SSPDR=c;
while(!(SSPSR & 0x04) );
c=SSPDR;
}
void SPIPutCharWithoutWaiting(unsigned char c){
SSPDR = c;
}
void SPI8Clocks(unsigned char nClocks){
while (nClocks--){
//while(!(SSPSR & 0x02) );
SSPDR =0xff;
SPIWait();
}
}
void delay_ms(int count){
int i;
count *= 3200;
for (i = 0; i < count; i++){
}
}
void spi_init(void){
SSPCR1 = 0;
PINSEL1 |= 0x000000A8;
PINSEL0 |= 0X00001500;
IODIR0 |= MMC_CS;
IOSET0 |= MMC_CS;
IODIR0 |= LCD_CS;
IOSET0 |= LCD_CS;
S0SPCR = 0x0000093C;
S0SPCCR = 0x00000008;
SSPCR0 = 0x0107; // cpol = 1, cpha = 1
SSPCPSR = 2; // prescale
SSPCR1 = 2; // enable
return;
}
void spi_command(int dat){
//int temp;
IOCLR0 |= LCD_CS;
S0SPDR = dat & ~0x0100;// & ~0x00000100; // send next SPI channel 0 data
// while ((SSPSR & 0x10)) ; // wait for transfer completed
while( (S0SPSR & 0x80 )==0) ;
IOSET0 |= LCD_CS;
}
void spi_data(int dat){
IOCLR0 |= LCD_CS;
S0SPDR = dat | 0x0100;// & ~0x00000100; // send next SPI channel 0 data
//while ((SSPSR & 0x10)) ; // wait for transfer completed
while( (S0SPSR & 0x80 )==0) ;
IOSET0 |= LCD_CS;
}
void feed(void)
{
PLLFEED=0xAA;
PLLFEED=0x55;
}
void InitBoard(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;
IODIR1 = 0x10000;
IOSET1 = 0x10000;
//spi_init();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -