#define __MAIN_C__
#include "STM8S105K4.h"
/*===========================================================================*/
#define DYMMY_BYTE 0x00
#define SPI_FLASH_READ 0x03 #define SPI_FLASH_FAST_READ 0x0B
void SPI_FLASH_CS_HIGH(void) { PE_ODR = PE_ODR|0x20; }
void SPI_FLASH_CS_LOW(void) { PE_ODR = PE_ODR&0xDF; }
//unsigned char SPI_FLASH_SendByte(unsigned char dat) unsigned char SPI_FLASH_SendByte(unsigned char dat)
{ //* Loop while DR register in not emplty * while(!(SPI_SR & 0x02));
//* Send byte through the SPI peripheral * SPI_DR = dat;
while(!(SPI_SR & 0x02));//判斷發(fā)射為不為空 //* Wait to receive a byte * while(!(SPI_SR & 0x01));
//* Return the byte read from the SPI bus * //SPI_DR= dat; /* while (!(SPI_SR& 0x02)); while(SPI_SR&0x80); */ return (unsigned char)(SPI_DR); }
/*===========================================================================*/
void STM8_CLK_Init(void) { CLK_CKDIVR = 0x00; /* 16MHz */ //CLK_SWCR = 0x02; //CLK_SWR = 0xB4; }
void STM8_GPIO_Init(void) { PE_DDR |= 0x20; PE_CR1 |= 0x20; }
void STM8_SPI_Init(void) { SPI_CR1 = 0x04; /* 0B10000101 */ SPI_CR2 = 0x03; /* 0B00000011 */ SPI_CRCPR = 0x07;
/* Enable the SPI peripheral*/ SPI_CR1 = SPI_CR1|0x40; // SPI_CR1 |= 0x07; //SPI_CR2 = 0x03; /* 0B00000011 */ }
void STM8_Init(void) { // asm("sim");
STM8_CLK_Init(); STM8_GPIO_Init(); STM8_SPI_Init();
// asm("rim"); }
/*===========================================================================*/
void STM8_UART2_SendByte(unsigned char dat) { //while(!UART2_SR_TXE); while(( UART2_SR&0x80)==0x00); UART2_DR = dat;
//asm("nop"); }
void DelayMS(unsigned int ms) { unsigned char i; while(ms!= 0) { for(i=0;i<250;i++); for(i=0;i<75;i++); ms--; } } void main(void) { unsigned char temp = 0,res;
//u32 id = 0, tic = 160;
STM8_Init(); while(1) { SPI_FLASH_CS_LOW(); SPI_FLASH_SendByte(0x06);
DelayMS(10); res = SPI_FLASH_SendByte(0x15);
if(res == 0xEA) {
} else {
}
SPI_FLASH_CS_HIGH(); } }