#include<p24FJ256GB106.h>
//#define CS PORTDbits.RD3
#define CS LATDbits.LATD3
#define HOLD VBUS
#define CS_TRIS TRISDbits.TRISD3
#define READ 0x03
#define WRITE 0x02
#define WRITE_ENABLE 0x06
#define WRITE_DISABLE 0x04
#define READ_STATUS 0x05
#define WRITE_STATUS 0x01
#define CLOCK_FREQ 32000000
#define BAUD_RATE 19200
//#define CLOCK_FREQ 8000000
//#define BAUD_RATE 19200
#define nl() ConsolePutROMString((const char*)"\r\n");
#define tab() ConsolePutROMString((const char*)" ");
#define PPSUnLock __builtin_write_OSCCONL(OSCCON & 0xbf)
#define PPSLock __builtin_write_OSCCONL(OSCCON | 0x40)
#define iPPSInput(fn,pin) fn=pin
#define PPSInput(fn,pin) iPPSInput(IN_FN_##fn,IN_PIN_##pin)
#define iPPSOutput(pin,fn) pin=fn
#define PPSOutput(fn,pin) iPPSOutput(OUT_PIN_##pin,OUT_FN_##fn)
#define OUT_FN_PPS_U2TX 5
#define OUT_PIN_PPS_RP14 RPOR7bits.RP14R
#define IN_PIN_PPS_RP29 29
#define IN_FN_PPS_U2RX RPINR19bits.U2RXR
#define OUT_FN_PPS_SDO3 32
#define OUT_PIN_PPS_RP4 RPOR2bits.RP4R
#define OUT_FN_PPS_SCK3OUT 33
#define OUT_PIN_PPS_RP3 RPOR1bits.RP3R
#define IN_PIN_PPS_RP23 23
#define IN_FN_PPS_SDI3 RPINR28bits.SDI3R
void delay_ms(unsigned int);
void SPI_init(void);
void init_eeprom(void);
unsigned char SPIPut(unsigned char);
void UART_Config_PPS(void);
unsigned char read_eeprom(unsigned int, unsigned char);
//void write_eeprom(unsigned int, unsigned char);
void spi_interrupt_init(void);
void write_enable_latch(void);
void ConsolePutROMString(const char* str);
void ConsolePut(unsigned char c);
void PrintChar(unsigned char);
const unsigned char CharacterArray[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2( IESO_OFF & PLLDIV_DIV2 & FNOSC_PRIPLL & FCKSM_CSDCMD & IOL1WAY_OFF & DISUVREG_OFF & POSCMOD_XT) //32MHZ
//_CONFIG2(FNOSC_PRI & IOL1WAY_OFF & POSCMOD_XT) //8MHZ
unsigned char read_eeprom(unsigned int address, unsigned char data)
{
unsigned char read = 0;
// HOLD = 1;
CS = 0;
read = SPIPut(READ);
read = SPIPut((address & 0xFF00) >> 8);
read = SPIPut(address & 0x00FF);
read = SPIPut(data);
CS = 1;
return (read);
}
void write_eeprom(unsigned int address, unsigned char data)
{
unsigned char write;
// HOLD = 1;
write_enable_latch();
delay_ms(10);
CS = 0;
write = SPIPut(WRITE);
write = SPIPut((address & 0xFF00) >> 8);
write = SPIPut(address & 0x00FF);
write = SPIPut(data);
CS = 1;
}
void write_enable_latch(void)
{
CS = 0;
SPIPut(WRITE_ENABLE);
CS = 1;
}
void ConsolePutROMString(const char* str)
{
unsigned char c;
while( (c = *str++) )
{
ConsolePut(c);
//put character by character on screen (transmit using usart)
}
}
void ConsolePut(unsigned char c)
{
while(U2STAbits.TRMT == 0);
//wait until previous transmission is complete or done
U2TXREG = c;
//write the data byte to lower byte of TXREG
}
void PrintChar(unsigned char toPrint)
{
unsigned char PRINT_VAR;
PRINT_VAR = toPrint;
toPrint = (toPrint>>4)&0x0F;
ConsolePut(CharacterArray[toPrint]);
toPrint = (PRINT_VAR)&0x0F;
ConsolePut(CharacterArray[toPrint]);
//????not exactly understood why this is done????
return;
}
void UART_Config_PPS()
{
PPSUnLock;
PPSInput(PPS_U2RX, PPS_RP29);
PPSOutput(PPS_U2TX,PPS_RP14);
PPSLock;
}
void SPI_Config_PPS()
{
PPSUnLock;
PPSOutput(PPS_SDO3,PPS_RP4);
PPSOutput(PPS_SCK3OUT,PPS_RP3);
PPSInput(PPS_SDI3,PPS_RP23);
PPSLock;
}
void delay_ms(unsigned int cnt)
{
volatile unsigned int i, b;
b = cnt;
while(b--)
{
for(i = 0; i < 3200; i++);
}
}
void spi_init()
{
CS_TRIS = 0;/*make chip select an output */
CS = 1; /*deselect the eeprom */
delay_ms(5);
SPI_Config_PPS();
delay_ms(5);
SPI3STATbits.SPIROV = 0;
SPI3CON2 = 0;
SPI3CON1 = (SPI3CON1 & 0xFE90) | 0x013A;
// SPI3CON1bits.MSTEN = 1;
// SPI3CON1bits.SMP = 1;
// SPI3CON1bits.CKP = 0;
// SPI3CON1bits.CKE = 1;
// SPI3CON1bits.SPRE = 0x06;
// SPI3CON1bits.PPRE = 0x02;
SPI3STATbits.SPIEN = 1;
// CS_TRIS = 0;/*make chip select an output */
// CS = 0; /*deselect the eeprom */
// SPI3CON1bits.MSTEN = 1;
// SPI3STATbits.SPIROV = 0;
// SPI3CON2bits.SPIBEN = 1;
// SPI3STATbits.SPIEN = 1;
SPI_Config_PPS(); // SPI3STATbits.SPIEN = 1;
}
unsigned char SPIPut(unsigned char val)//val is byte that need to be transfered
{
unsigned char dummy = 0;
//IFS5bits.SPI3IF = 0; /* Clear the SPIxIF bit in the respective IFSx registeri */
dummy = SPI3BUF; /* read the value before writing to avoid overflow bit setting */
SPI3BUF = val; /* write in to buffer the new value */
//while(IFS5bits.SPI3IF != 1);/* WAIT FOR TRANSFER TO COMPLETE */
while(!SPI3STATbits.SPIRBF);
return SPI3BUF;
}
#if 0
void __attribute__((__interrupt__,no_auto_psv))_SPI3Interrupt(void)
{
_RF5 = !_RF5;
IFS5bits.SPI3IF = 0;
}
void spi_interrupt_init(void)
{
IFS5bits.SPI3IF = 0;
IPC22bits.SPI3IP = 0x03;
IEC5bits.SPI3IE = 1;
}
#endif
void UART_init()
{
U2BRG = (CLOCK_FREQ/2/16)/BAUD_RATE-1;
U2STA = 0;
U2MODEbits.UARTEN = 1;
//U2STAbits.UTXEN = 1;
U2STAbits.UTXEN = 1;
delay_ms(5);
UART_Config_PPS();
}
void transmit( unsigned char val)
{
while(!U2STAbits.TRMT);
U2TXREG = val;
}
int main()
{
unsigned char eread = 0x00;
unsigned int i;
unsigned char j = 0x00, k = 0;
AD1PCFGL = 0xffff;
AD1PCFGH = 0x0003;
_TRISF5 = 0;
_RF5 = 0;
delay_ms(5);
spi_init();
delay_ms(5);
UART_init();
delay_ms(5);
while(1)
{
#if 0
for(i = 0x0000; i < 64; i++)
{
write_eeprom(i , j++);
delay_ms(10);
}
#endif
k = 0;
for(i = 0x0000; i < 64; i++)
{
if(k < 32)
{
eread = read_eeprom(i, 0x00);
delay_ms(10);
PrintChar(eread);
tab();
k++;
if( k == 32)
{
k = 0;
nl();
}
}
}
nl();
j = 0;
delay_ms(100);
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
The EEPROM code to write and Read to PIC24F using xc16 compiler. Complete code is written in detail you can understand easily . EEPROM Write READ code of 32 Bytes.
资源推荐
资源详情
资源评论
收起资源包目录
EEPROM.zip (9个子文件)
EEPROM
eeprom25LA256.c 6KB
main.hex 8KB
main.hex~ 8KB
acciybt detauks 17B
main.elf 52KB
real.hex~ 9KB
Makefile 283B
main.o 27KB
real_final.hex~ 9KB
共 9 条
- 1
资源评论
JonSco
- 粉丝: 66
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功