Jump to content
Sign in to follow this  
nima

Spi Doesn't Work In Pic16f1827

Recommended Posts

Hi,

 

I'm porting my code from HI-TECH and it works fine in HI-TECH but the SDO1 is not working correctly in BoostC. Whenever I enable SPI1 SDO1 goes to 0V instead of 5V. Also looking at the header file for 1827 I see all the variables are

volatile char ssp1con1 @SSP1CON1;

instead of

volatile unsigned char ssp1con1  @SSP1CON1;

is this correct?

 

Here is the code I'm running:

//Include files
#include <system.h>
#include "rep_batt.h"
//#include "usart.h"
#include <stdio.h>
#include <string.h>

#pragma	CLOCK_FREQ	8000000 //8Mhz

#pragma DATA _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _FCMEN_OFF & _IESO_OFF  //doesn't exist
#pragma DATA _CONFIG2, _PLLEN_OFF & _LVP_OFF & _STVREN_OFF & _WRT_OFF & _BORV_19

//Init EEPROM with default tagid of all FF
#pragma DATA _EEPROM, 0xAA, 0xAA, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF

//USART SETTING
#define FOSC 8000000
#define spBAUD 9600
#define fSPBCLK (FOSC)	// UART Baud rate generator clock (high speed)
#define SPBRG_VAL ((fSPBCLK / (spBAUD * 16L)) - 1L)

// bit_time FOSC / 4 / BAUDRATE 
#define bit_time 208 // 9600 baud at 8MHz

// hardware USART support
#define TX_PORT 	PORTB
#define TX_TRIS 	TRISB
#define TX_BIT 		5
#define RX_PORT 	PORTB
#define RX_TRIS 	TRISB
#define RX_BIT 		2
#define e_SPBRG		SPBRG
#define e_RCREG 	RCREG
#define e_TXREG 	TXREG
#define e_TXSTA 	TXSTA
#define e_RCSTA 	RCSTA
#define e_TXIF_PIR 	PIR1
#define e_RCIF_PIR 	PIR1
#define e_TXIF_BIT 	TXIE
#define e_RCIF_BIT 	RCIE
#define MODE (USART_reset_wdt | USART_HW)

#include <rs232_driver.h>

typedef unsigned char Byte;
volatile static Byte RcvBuffer[64];
volatile static Byte TxBuffer[64];
volatile short RcvCounter = 0;
volatile unsigned short TxCounter = 0;
volatile short boolean = 0;
volatile Byte spi_pointer = 0;

Byte getSPIChar(void);
void sendSPIChar(Byte ch);
short SPIchrRdy(void);
short chkSPIOverflow(void);
short chkSPICollision(void);
short chkSPIBusCollision(void);

short chkSPIBusCollision(void){
if(pir1.BCL1IE){
	pir1.BCL1IE = 0;
	return 1;
}
return 0;
}

short chkSPICollision(void){
if(ssp1con1.WCOL){
	ssp1con1.WCOL = 0;
	return 1;
}
return 0;
}

short chkSPIOverflow(void){
if(ssp1con1.SSPOV){
	ssp1con1.SSPOV = 0;
	return 1;
}
return 0;
}

short SPIchrRdy(void){
if(RcvCounter>0)
	return 1;
return 0;
}

void interrupt(void){

if((pie1.SSP1IE)&&(pir1.SSPIF)){

	//PORTBbits.RB1 = 0;		//SDI TRIS bit output
	//boolean = 1;

	if(TxCounter>=0){			
		//SSP1BUF = TxBuffer[TxCounter];			

		ssp1buf = TxBuffer[spi_pointer];

		--TxCounter;
		++spi_pointer;						
	}
	//PORTBbits.RB1 = 1;		//SDI TRIS bit output
	else{
		RcvBuffer[RcvCounter++] = ssp1buf;
		RcvBuffer[RcvCounter] = 0;		
	}
	pir1.SSPIF = 0; //clear interrupt bit	
}
}

Byte getSPIChar(void){	
return ssp1buf;
}
/**
void sendSPIChar(Byte ch){	

//PORTBbits.RB1 = 0;		//SDI TRIS bit output
// output one byte 
while(!SSP1IF) //!SSP1STATbits.BF)
	continue;
SSP1BUF = ch;

//PORTBbits.RB1 = 1;		//SDI TRIS bit input
//Byte dummy = getSPIChar();
}
**/

void main(void){
uart_init(1,SPBRG_VAL);
unsigned char input;
ssp1con1 = 0x00;
/**
CCP1CON = 0x00;	
CCP2CON = 0x00;
CCP3CON = 0x00;
CCP4CON = 0x00;

CLKRCON = 0x00; //Disable reference clock control
**/
//INTCON=0;		// purpose of disabling the interrupts.
/** interrupts for peripherals **/
intcon.GIE = 1;
intcon.PEIE = 1;

/** peripheral 1 control bits **/
pir1.SSPIF = 0; //clear interrupt bit
pie1.SSP1IE = 1; //SSP1 interrupt
//	pie1.RCIE = 1;   //USART RCV
//	pie1.TXIE = 1;	 //USART TX

anselb = 0x00;	// disable analog ports on port B
ansela = 0x00;	// disable analog ports on port A


// Set clock prescaler (8MHz)
osccon.IRCF0 = 0; //0
osccon.IRCF1 = 1; //1
osccon.IRCF2 = 1; //1
osccon.IRCF3 = 1; //1	
//	osccon.SCS0 = 0;	//clear to enable 32Mhz
//	osccon.SCS1 = 1;	//clear to enable 32Mhz
//	osccon.SPLLEN = 1;

//Configure usart pin
apfcon0.RXDTSEL = 1;	//0:RX is on RB1 1:RX is on RB2
apfcon1.TXCKSEL = 1;	//0:TX is on RB2 1:TX is on RB5

apfcon0.SDO1SEL = 1; //0:SDO1 is on RB2; 1:SDO1 is on RA6	

ssp1con1.SSPEN = 1;
ssp1con1.CKP = 0;
ssp1con1.SSPM0 = 1;
ssp1con1.SSPM1 = 0;
ssp1con1.SSPM2 = 1;
ssp1con1.SSPM3 = 0;

//ssp1con1 = 0x25; 
//SSP1STAT
ssp1stat.SMP = 0;	//sample must be disabled in slave mode
ssp1stat.CKE = 0;	//clock edge occurs from idle to active clock state 

//SDI, SDO, SCK TRIS setup
lata.LATA6 = 1;			//SDO PortA bit set to > V1H
trisa.TRISA6 = 0;			//SDO TRIS bit clear output		

portb.RB1 = 1;			//SDI PortB bit set to > VIH
trisb.TRISB1 = 0;		//SDI TRIS bit set input

portb.RB4 = 1;			//SCK (Slave mode) bit set < V1H	
trisb.TRISB4 = 1;		//SCK TRIS bit clear input

//led on board
//	set_bit(porta,RA6);
//	clear_bit(trisa,TRISA6);

porta.RA4 = 1;
trisa.TRISA4 = 0;

//	porta = 0xFF;
//	trisa = 0x00;
unsigned int counter = 0;

// Output a message to prompt the user for a keypress	
puts("Press a key and I will echo it back:");
while(1){

	if(counter == 1000){
		//SSP1BUF = 'A';
		counter = 0;
	}
	else{			
		if(counter % 2 == 0){
			trisa.TRISA4 = 1;				
			delay_s(1);
			//puts("I'm here!!");
		}
		else{
			trisa.TRISA4 = 0;				
			delay_s(1);
		}
		++counter;
	}


	if(kbhit()){			
		input = getc();	// read a response from the user
		if(input == 'z'){
			spi_pointer = 0;
			TxCounter = 0;				
		}
		else{
			//sendSPIChar(input);
			TxBuffer[TxCounter] = input;
			//for(i=1; i<64; i++)
			//	TxBuffer[i] = 'A';
			++TxCounter;
		}
		//puts("I detected [%c]\r\n",input);	// echo it back
		puts("I detected [");
		putc(input);
		puts("]");	// echo it back
		//puts("boolean is %x\r\n",boolean);
		boolean = 0;
	}
	if(chkSPIBusCollision())
		puts("SPI bus collision detected!\r\n");

	if(chkSPIOverflow())
		puts("SPI overflow detected!\r\n");

	if(chkSPICollision())
		puts("SPI collision detected!\r\n");

	/**
	if(SPIchrRdy()){
		input = getSPIChar();
		puts("%c",input);	
	}
	**/

	if(RcvCounter>0){
	//******printf doesn't work!!
//			printf("spi output: %s\r\n", RcvBuffer);
		RcvCounter = 0;
	}	

	//__delay_us(17.1);			
}
}

 

Thanks!

Share this post


Link to post
Share on other sites

nima,

Also looking at the header file for 1827 I see all the variables are
volatile char ssp1con1 @SSP1CON1;

instead of

volatile unsigned char ssp1con1  @SSP1CON1;

is this correct?

The default signage of a 'char' is unsigned. A compiler command line option can be used to make the signage of a plain char to be signed. So I guess this is not your problem.

 

Regards

Dave

Share this post


Link to post
Share on other sites
nima,
Also looking at the header file for 1827 I see all the variables are
volatile char ssp1con1 @SSP1CON1;

instead of

volatile unsigned char ssp1con1  @SSP1CON1;

is this correct?

The default signage of a 'char' is unsigned. A compiler command line option can be used to make the signage of a plain char to be signed. So I guess this is not your problem.

 

Regards

Dave

 

Thanks for the response about the variables Dave. But no, that doesn't solve my problem with the SPI not working. Dave do you have any ideas I could try? Since this code works in HI-TECH but not in SourceBoost does that mean there could be a bug in SourceBoost? Anybody else running into this problem with the 1827 chip?

 

I forgot to add this to my original post but whenever I import the hex file into PICkit 2 Programmer I get the following message "Warning: Hex File Loaded is larger than device". Could this be causing the problem?

 

Nima

Share this post


Link to post
Share on other sites
nima,
Also looking at the header file for 1827 I see all the variables are
volatile char ssp1con1 @SSP1CON1;

instead of

volatile unsigned char ssp1con1  @SSP1CON1;

is this correct?

The default signage of a 'char' is unsigned. A compiler command line option can be used to make the signage of a plain char to be signed. So I guess this is not your problem.

 

Regards

Dave

 

Dave I figured the SPI problem out. SDO1 is kept low until data is transmitted after that it will stay high. Now the only other problem is the warning error saying hex file is too big even though in SourceBoost IDE the code size shows the following:

 

Building CASM file

Memory Usage Report

===================

RAM available:384 bytes, used:193 bytes (50.3%), free:191 bytes (49.7%),

Heap size:187 bytes, Heap max single alloc:79 bytes

ROM available:4096 words, used:591 words (14.5%), free:3505 words (85.5%)

 

Thanks,

Nima

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...