|
|
View previous topic :: View next topic |
Author |
Message |
spilz
Joined: 30 Jan 2012 Posts: 219
|
SPI : why my function works, but not the PICC function |
Posted: Tue Oct 25, 2016 11:26 am |
|
|
hello,
I'm trying to use the CCS's SPI functions but it does not work, and with "my" SPI functions it works well ...
I guess I misunderstand Something but I don't know what ...
I'm using 16F690 with RFM69 module in SPI :
My code with "my" function :
Code: | #define CLOCK_MHZ 20
#define QUARTZ_MHZ 20
#include <16F690.h>
#device ADC=8
#FUSES NOWDT,PROTECT,BROWNOUT,NOMCLR,NOCPD,NOPUT
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#use delay(crystal=CLOCK_MHZ MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,restart_wdt,errors,TIMEOUT = 100,stream = RS232_PICKIT2)
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,bits=8,errors,TIMEOUT = 100,STREAM = RS232_UART)
#define RFM69_MISO_PIN PIN_B4
#define RFM69_MOSI_PIN PIN_C7
#define RFM69_CLK_PIN PIN_B6
#define RFM69_CS_PIN PIN_C2
#define RFM69_RST_PIN PIN_C6
#define RFM69_Select output_low(RFM69_CS_PIN)
#define RFM69_UnSelect output_high(RFM69_CS_PIN)
///////////////////////////////////////////////////////////
//Name: SPICmd8bit
//Function: SPI Write one byte
//Input: WrPara
//Output: none
//note: use for burst mode
///////////////////////////////////////////////////////////
void SPICmd8bit(int WrPara){
byte bitcnt = 0;
RFM69_Select;
output_low(RFM69_CLK_PIN);
for(bitcnt=8; bitcnt!=0; bitcnt--){
output_low(RFM69_CLK_PIN);
if(WrPara&0x80)
output_high(RFM69_MOSI_PIN);// SDI = 1;
else
output_low(RFM69_MOSI_PIN);// SDI = 0;
output_high(RFM69_CLK_PIN);
WrPara <<= 1;
}
output_low(RFM69_CLK_PIN);
output_high(RFM69_MOSI_PIN);// SDI = 1;
}
///////////////////////////////////////////////////////////
//Name: SPIRead8bit
//Function: SPI Read one byte
//Input: None
//Output: result byte
//Note: use for burst mode
///////////////////////////////////////////////////////////
int SPIRead8bit(void){
byte RdPara = 0;
byte bitcnt = 0;
RFM69_Select;
output_high(RFM69_MOSI_PIN);// SDI = 1; //Read one byte data from FIFO, MOSI hold to High
for(bitcnt=8; bitcnt!=0; bitcnt--){
output_low(RFM69_CLK_PIN);
RdPara <<= 1;
output_high(RFM69_CLK_PIN);
if(input(RFM69_MISO_PIN))
RdPara |= 0x01;
else //Codi per realitzar un retard del nombre especificat de cycles
delay_cycles(1);//d'instruccio (1-255). Un cicle d'instrucció és igual
//a quatre cicles de l'oscil·lador.
//OSC16MHz --> (1*4)/16 = 0.25uS
}
output_low(RFM69_CLK_PIN);
return(RdPara);
}
void main()
{
int value = 0;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_wdt(WDT_OFF);
disable_interrupts(GLOBAL);
fprintf(RS232_PICKIT2,"16F690 RFM69\r\n");
output_low(RFM69_RST_PIN);
delay_ms(10);
fprintf(RS232_PICKIT2,"SPI ");
SPICmd8bit(0x2F|0x80);
SPICmd8bit(0xAA);
RFM69_UnSelect;
delay_ms(10);
SPICmd8bit(0x2F); //Send address first
value = SPIRead8bit();
RFM69_UnSelect;
if(value == 0xAA)
fprintf(RS232_PICKIT2,"OK\r\n");
else
fprintf(RS232_PICKIT2,"ERROR\r\n");
} |
It works well,
but when I try this :
Code: | #define CLOCK_MHZ 20
#define QUARTZ_MHZ 20
#include <16F690.h>
#device ADC=8
#FUSES NOWDT,PROTECT,BROWNOUT,NOMCLR,NOCPD,NOPUT
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#use delay(crystal=CLOCK_MHZ MHz)
#use rs232(baud=9600,parity=N,xmit=PIN_A0,rcv=PIN_A1,bits=8,restart_wdt,errors,TIMEOUT = 100,stream = RS232_PICKIT2)
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B5,bits=8,errors,TIMEOUT = 100,STREAM = RS232_UART)
#use spi (MASTER, DO=PIN_C7, DI=PIN_B4, CLK=PIN_B6, MODE=0, BITS=8, baud=115200)
#define RFM69_MISO_PIN PIN_B4
#define RFM69_MOSI_PIN PIN_C7
#define RFM69_CLK_PIN PIN_B6
#define RFM69_CS_PIN PIN_C2
#define RFM69_RST_PIN PIN_C6
#define RFM69_Select output_low(RFM69_CS_PIN)
#define RFM69_UnSelect output_high(RFM69_CS_PIN)
void main()
{
int value = 0;
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_wdt(WDT_OFF);
disable_interrupts(GLOBAL);
fprintf(RS232_PICKIT2,"16F690 RFM69\r\n");
output_low(RFM69_RST_PIN);
delay_ms(10);
fprintf(RS232_PICKIT2,"SPI ");
spi_xfer(0x2F|0x80);
spi_xfer(0xAA);
RFM69_UnSelect;
delay_ms(10);
spi_xfer(0x2F); //Send address first
value = spi_xfer(0);
RFM69_UnSelect;
if(value == 0xAA)
fprintf(RS232_PICKIT2,"OK\r\n");
else
fprintf(RS232_PICKIT2,"ERROR\r\n");
} |
it does not work ...
someone know why ?
What I miss understand ?
Thanks for your help
regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Oct 25, 2016 11:41 am |
|
|
You are forgetting to select the chip....
Your code has the select line inside the routines. The CCS code does not. You are deselecting the chip after the transactions, but not selecting it before..... |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Tue Oct 25, 2016 11:56 am |
|
|
that works
really stupid error ^^
and what about using ENABLE in SPI ?
Code: | #use spi (MASTER, SPI1, MODE=0, BITS=8,ENABLE=PIN_C2,ENABLE_ACTIVE=0, baud=115200) |
why it does not work ?
regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Tue Oct 25, 2016 12:50 pm |
|
|
Potentially yes.
You need to set the line high at the start of the code, and may need to add a delay between the enable and clocking (enable_delay in the #use). |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Tue Oct 25, 2016 1:54 pm |
|
|
Enable_delay is not available for hardware SPI
But all other works fine
Thanks for your help |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Wed Oct 26, 2016 1:32 am |
|
|
The reason the hardware CS won't work for you is not the delay, but the way the chip expects the select to work. It wants it to stay low for the whole transaction. The ENABLE when using the CCS library operates on a per byte basis. It's designed for chips which use this (a byte enable), to ensure byte alignment, not to operate the CS. Assumes it you need a CS, you will be operating it 'manually' (in code).... |
|
|
spilz
Joined: 30 Jan 2012 Posts: 219
|
|
Posted: Wed Oct 26, 2016 1:36 am |
|
|
OIC
I understand now, I'm going to do it 'manually'
thanks |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|