Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 #include<P18F25k22.h> #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<delays.h> #include <eep.h> /////////////////////////////////////////////////////////////////////////////////////// void sec1_delay(void); void spi(unsigned char); //SPI Function To send the data void delay_10sec(void); //delay void main(void) { INTCONbits.GIE=1; // Enable the global Interrupt TRISCbits.RC3 =0; //sclk1 o/p PORTCbits.RC3 = 0; TRISCbits.RC5 =0; //sdo o/p PORTCbits.RC5 = 0; TRISCbits.RC4 =1; //sdi i/p PORTCbits.RC4 = 1; TRISAbits.RA5 =0; //SS PORTAbits.RA5 = 0; TRISBbits.RB5 = 0; // LED PORTBbits.RB5 = 0; TRISBbits.RB6 = 0; // LED PORTBbits.RB6 = 0; SSP1STAT =0x40; SSP1CON1=0x22; //Configure SSPCON1 for ENABLE SERIAL PORT // and disable general I/O pin ,, SPI master clock= Fosc/64 // we are only sending the data so we do't need to set SPI pin as input while(1) // loop for ever so that led keep repeating that pattern { PORTBbits.RB5 = 1; // 1 LED to check it is in while spi(1); // send 01 hex to the SPI port it will glow the first led Delay10KTCYx(400); // wait for approximate 1sec , //if we do't give the delay then led will flash too fast that // we can't even know when it glow and when it off spi(0); //send 0 to turn of slave LED Delay10KTCYx(400); // wait for 1 sec } } void spi(unsigned char myData) { PORTBbits.RB6 = ~PORTBbits.RB6; // second LED to see function is executed. SSP1BUF = myData; // put the data in the SSPBUF register which going to be send while(!SSP1STATbits.BF); // wait until the all bits sended } void sec1_delay(void) { BY_PASS_RESET=0; BY_PASS_SET=1; Delay10KTCYx(60); BY_PASS_SET=0; delay_10sec(); delay_10sec(); BY_PASS_RESET=1; BY_PASS_SET=0; Delay10KTCYx(60); BY_PASS_RESET=0; delay_10sec(); delay_10sec(); } void delay_10sec(void) { Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); Delay10KTCYx(200); }
Hi,
A scope of all the signals could help to identify the problem.
Klaus
#include<P18F25k22.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<delays.h>
#include <eep.h>
#include <spi.h>
void spi(unsigned char); //SPI Function To send the data
#pragma code // end of the code
/*-------------------------*/
void main(void)
{ //SSP1BUF = 0;
TRISCbits.RC3 =1; //sclk1 i/p
LATCbits.LATC3 = 1;
TRISCbits.RC5 =0; //sdo o/p
LATCbits.LATC5 = 0;
TRISCbits.RC4 =1; //sdi i/p
LATCbits.LATC4 = 1;
TRISAbits.RA5 = 1; // SS i/p
TRISBbits.RB5 = 0;
LATBbits.LATB5 = 1;
TRISBbits.RB6 = 0;
LATBbits.LATB6 = 1;
INTCONbits.GIE=1;
SSP1STAT =0x00;
SSP1CON1=0x25;
while(1)
{
if(SSP1STATbits.BF == 1)
{
PORTBbits.RB5 = ~PORTBbits.RB5;
PORTBbits.RB6 = SSP1BUF;
}
}
}
Slow down. Turning on the LED with a SPI command is the last piece of the chain.sir i'm unable to turn the LED on
#include<P18F25k22.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<delays.h>
#include <eep.h>
#include <spi.h>
void spi(unsigned char); //SPI Function To send the data
void main(void)
{
TRISCbits.RC3 =0; //sclk1 o/p
TRISCbits.RC5 =0; //sdo o/p
TRISCbits.RC4 =1; //sdi i/p
TRISCbits.RC2 =0; //SS
TRISBbits.RB5 = 0; // LED
LATBbits.LATB5 = 0;
TRISBbits.RB6 = 0; // LED
LATBbits.LATB6 = 0;
SSP1STAT =0x40; // Transmit occurs on transition from active to Idle clock state
SSP1CON1=0x22; //Configure SSPCON1 for ENABLE SERIAL PORT
// and disable general I/O pin ,, SPI master clock= Fosc/64
INTCONbits.GIE=1; // Enable the global Interrupt
while(1) // loop for ever so that led keep repeating that pattern
{
PORTBbits.RB5 = 1; // led
PORTCbits.RC2 = 0;
spi(0x9F); // send hex to the SPI port
PORTCbits.RC2 = 1;
}
}
void spi(unsigned char myData)
{
PORTBbits.RB6 = ~PORTBbits.RB6; //led
SSP1BUF = myData; // put the data in the SSPBUF register which going to be send
while(!SSP1STATbits.BF); // wait until the all bits sended
PIR1bits.SSP1IF = 1; // clear interrupt
}
to togle bit you should use
LATBbits.RB5 = ~PORTBbits.RB5;
instead of
PORTBbits.RB5 = ~PORTBbits.RB5;
Guessing?it would be better idea then guess from oscilloscope
You will only get the LSB used so I'm not sure what the point is on sending 0x9f (except for testing to see the SDO line changing which is certainly useful).
void main(void)
{
TRISBbits.RB4 = 0; // LED
LATBbits.LATB4 = 0;
TRISCbits.RC3 =0; //sclk1 o/p
TRISCbits.RC5 =0; //sdo o/p
TRISCbits.RC4 =1; //sdi i/p
TRISCbits.RC2 =0; //SS
TRISBbits.RB5 = 0; // LED
TRISBbits.RB6 = 0; // LED
SSP1STAT =0x40; // Configure SSPSTAT for transmission occur from idle to active clock and Buffer flag =0
SSP1CON1=0x22; //Configure SSPCON1 for ENABLE SERIAL PORT
// and disable general I/O pin ,, SPI master clock= Fosc/64
INTCONbits.GIE=1; // Enable the global Interrupt
while(1) // loop for ever so that led keep repeating that pattern
{
LATBbits.LATB5 = 1; //led
LATCbits.LATC2 = 0; // ss bar
spi(0x95); // send 01 hex to the SPI port it will glow the first led
spi(0x65);
LATCbits.LATC2 = 1;
Delay10TCYx(9); // 36us delay
}
}
void spi(unsigned char myData)
{
LATBbits.LATB6 = ~LATBbits.LATB6;
SSP1BUF = myData;
SSP1BUF = myData; // put the data in the SSPBUF register which going to be send
while(!SSP1STATbits.BF); // wait until the all bits sended
PIR1bits.SSP1IF = 1; // clear interrupt
}
#include<P18F25k22.h>
#include<delays.h>
#include <spi.h>
#pragma code
void main(void)
{
ANSELA = 0x00; // digital input
ANSELB = 0x00;
TRISCbits.RC3 =1; //sclk1 i/p
TRISCbits.RC5 =0; //sdo o/p
TRISCbits.RC4 =1; //sdi i/p
TRISAbits.RA5 = 1; // SS i/p
TRISB = 0;
LATB = 0;
SSP1STAT =0x40;
SSP1CON1=0x24;
INTCONbits.GIE=1;
while(1)
{
LATBbits.LATB5 = 1; //led
if(SSP1STATbits.BF)
{
PORTB = SSP1BUF;
SSP1BUF = 0;
}
}
}
while(1)
{
LATBbits.LATB5 = 1; //led
if(SSP1STATbits.BF)
{
PORTB = SSP1BUF;
SSP1BUF = 0;
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?