📄 spi2508.c
字号:
///////////////////////////////////////////////////////////////////////////////
// MPLAB IDE V7.11 + CCS C3.18
// by zhongwei
// 2009/05/10
///////////////////////////////////////////////////////////////////////////////
#if defined(__PCH__)
#include <18F452.h>
#device *=8
#device adc=10
#opt 10
#use delay(clock = 4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
//#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
#else
#include <16F877A.h>
#device *=16
#device adc=10
#use delay(clock = 4000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
//#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
#endIf
///////////////////////////////////////////////////////////////////////////////
#byte INTCON = 0x0B//
#byte PIE1 = 0x8C
#byte PIR1 = 0x0C
#byte ADCON1 = 0x9F
#byte TRISA = 0x85
#byte TRISC = 0x87
#byte PORTC = 0x07
#byte SSPBUF = 0x13
#byte SSPCON = 0x14
#byte SSPSTAT = 0x94
#bit SMP = SSPSTAT.7 //sample mode
#bit CKE = SSPSTAT.6//edge select
#bit BF = SSPSTAT.0 //
#bit SSPEN = SSPCON.5
#bit CKP = SSPCON.4 //
#bit SSPIF = PIR1.3 //
#define CS PIN_B2
#define sck PIN_B3//soft
#define SDI PIN_B4
#define SDO PIN_B5
//instruction
#define I_wren 0x06
#define I_wrdi 0x04
#define I_rdsr 0x05
#define I_wrsr 0x01
#define I_read 0x03
#define I_write 0x02
#define eeprom_addr int16
//#include "25c080.c"
///////////////////////////////////////////////////////////////////////////////
//seg code '0' '1' '2' '3' '4' '5' '6' '7' '8' '9' off -
const int8 DIsp_led_cc[]={ 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00, 0x40};//common chnode
const int8 DIsp_led_ca[]={ 0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff};//common anode
int8 count;
////////////////////////////////////////////////////////////////////////////////
void init_mcu(void)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4);
//setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_SCK_DIV_4);// NG??????????
setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// enable_interrupts(int_RTCC);
// enable_interrupts(global);
}
///////////////////////////////////////////////////////////////////////////////
void spi_out(int8 dat)
{
sspbuf=dat;
while(!sspif);//ok
sspif=0;
}
///////////////////////////////////////////////////////////////////////////////
int8 spi_in()
{
//int8 data;
sspbuf=0;//must (generate pulse)
while(!sspif);
sspif=0;
// data=sspbuf;
return(sspbuf);
}
///////////////////////////////////////////////////////////////////////////////
void init_ext_eeprom()
{
short int i;
output_high(CS);
output_low(SDI);
output_low(SCK);
output_low(SDO);
}
///////////////////////////////////////////////////////////////////////////////
int1 ext_eeprom_ready()
{
int8 data;
output_low(CS);
spi_write(0x05); //rdsr instruction
data=spi_read();
output_high(CS);//opration complete must set cs high
return !bit_test(data, 0); //return wip
}
#if defined spi_library//ng?????????????????????????????????????????????????
///////////////////////////////////////////////////////////////////////////////
void write_ext_eeprom(int8 page, int8 addr, int8 dat)
{
output_low(CS);//
spi_write(0x06); //wren
output_high(CS); //must
delay_us(10);
output_low(CS);
spi_write(0x02); //write instruction
spi_write(page); //high 8 bits
spi_write(address);
spi_write(dat);
output_high(CS);//opration complete must set cs high
}
///////////////////////////////////////////////////////////////////////////////
int8 read_ext_eeprom(int8 page, int8 address)
{
int8 data;
//while(!ext_eeprom_ready());
output_low(CS);
spi_write(0x03);//read eeprom instruction
spi_write(page);//high 8 bits
spi_write(address );
data=spi_read(0);
output_high(CS);
return(data);
}
#else //ok
///////////////////////////////////////////////////////////////////////////////
void write_ext_eeprom(int16 addr, int8 dat)
{
//int8 i_write;
output_low(CS);//select chip
spi_write(0x06); //wren
output_high(CS); //must
//delay_us(10);
output_low(CS);
spi_out(0x02);//write instruction
spi_out((int8)(addr>>8));//high 8 bits
spi_out((int8)(addr));
spi_out(dat);
output_high(CS);//opration complete must set cs high
}
///////////////////////////////////////////////////////////////////////////////
int8 read_ext_eeprom( int16 addr)
{
int8 data;
//while(!ext_eeprom_ready());
output_low(CS);
spi_out(0x03);//read eeprom instruction
spi_out((int8)(addr>>8));//high 8 bits
spi_out((int8)(addr));
data=spi_in();
output_high(CS);
return(data);
}
#endif
////////////////////////////////////////////////////////////////////////////////
void main ()
{
int16 i=1,j,recdata;
int16 adr;
/*
// library spi ng????????????????????????????
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4);
// init_ext_eeprom();
//while(1)
{
//spi_write(DIsp_led_cc[i++]);
//delay_ms(1000);
for(i=0;i<15;i++)
{
WRITE_EXT_EEPROM(2,i,i);
delay_ms(500);
}
}
*/
//self_hard spi ok
trisc=0xd7;
sspstat=0x00;
sspcon=0x30;
set_timer0(81); //timer0 initial value
//while(1)
{
////////////////////////////write
for(i=0;i<1024;i++)
{
WRITE_EXT_EEPROM(i,i);
delay_ms(1);
}
///////////////////////////read
for(i=0;i<1024;i++)
{
printf("%u\n\r",READ_EXT_EEPROM(i));
delay_ms(1);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -