by David Knott
#include <16C84.h> #pragma use delay (clock=4000000) // 4 Mhz clock. Used by RS232 and delay functions #pragma case // Makes the compiler case sensitive. Case insensitivity is the default! #pragma use fast_io(A) // The default I/O attribute is standard_io. This setting #pragma use fast_io(B) // has the compiler generate instructions to change the // data direction register during I/O operations. // fast_io does not generate these extra instructions. #pragma use RS232 (baud=9600, xmit=PIN_B0, rcv=PIN_B1) // This structure is overlayed on to an I/O port to gain access to the LCD pins. // The bits are allocated from low order up. struct { int unused:2; // The 232 serial port PB[0:1] int mosi:1; // Output PB2 int miso:1; // Input PB3 int sclk:1; // Output PB4 int cs:1; // Output PB5 } SPIPort; #pragma byte SPIPort=0x06 // Place the entire structure over PORTB (address 6) BYTE SPIPutch (BYTE byOutData) { int nIndex; BYTE byInData = 0; // Data is presented on rising edge of SCLK, and captured on falling edge of SCLK. // Tell the device we are initiating communications. SPIPort.cs = 0; for (nIndex = 0; nIndex < 8; ++nIndex) { // Update pin status to reflect bit in data to be sent. SPIPort.mosi = (byOutData & 0x80 ? 1 : 0); // Increment to next bit to be sent. byOutData = byOutData << 1; // Clock a rising edge on SCLK. MOSI presents on the rising edge of SCLK. SPIPort.sclk = 1; // TODO: Add delay to wait for slave device to present data? delay_cycles (20); byInData = byInData << 1; // Read what the slave device has presented on our MISO pin. if (SPIPort.miso) byInData |= 0x01; // We read what the slave device had asserted on MISO. SPIPort.sclk = 0; } SPIPort.cs = 1; return byInData; } void FlashLED (BYTE byOnTime) { output_low (PIN_A0); DELAY_MS (byOnTime); output_high (PIN_A0); } void main (void) { char chInChar; BYTE test = 0xff; BYTE byReturn; SPIPort.cs = 1; set_tris_b (0x0a); // PORTA is an output. set_tris_a (0x00); chInChar = 0; // Make sure sclk is low before we assert the Chip Select. SPIPort.sclk = 0; // printf ("Ready for input...\r\n"); if ( while (1) { chInChar = GETCHAR (); // chInChar++; byReturn = SPIPutch (chInChar); printf ("0x%x\r\n",byReturn); } }
Questions:
I am a begginer in Embedded systems field.I am currently working on PIC16F913 and trying to establish serial communication between two wireless modules using this PIC .I am unable to establish it by the following code:#include <16f913.h> #byte osccon=0x8f #byte cmcon0=0x9c #byte ansel=0x91 #byte port_a=5 #byte port_c=7 #byte port_b=6 #byte port_e=9 #byte lcdps=0x108 #byte lcdse0=0x11c #byte lcdse1=0x11d #byte lcdcon=0x107 #byte lcddata0=0x110 #byte lcddata1=0x111 #byte lcddata3=0x113 #byte lcddata4=0x114 #byte lcddata6=0x116 #byte lcddata7=0x117 #byte lcddata9=0x119 #byte lcddata10=0x11a #byte pir1=0x0c #byte pie1=0x8c #byte tmr1h=0x0f #byte tmr1l=0x0e #byte eecon1=0x18c #byte eecon2=0x18d #byte eeadrl=0x10d #byte eeadrh=0x10f #byte eedatl=0x10c #byte eedath=0x10e #byte intcon=0x0b #byte t1con=0x10 #byte tmr0=0x01 #byte option_reg=0x81 #byte trisb=0x86 #byte trisc=0x87 #byte trisa=0x85 #byte sspcon=0x14 #byte sspstat=0x94 #byte sspbuf=0x13 #byte intcon=0x0b #use delay(clock=8000000) void main() { osccon =0x71;/*internal osc freq. 8 Mhz*/ cmcon0 =0x07;/*make the comparators off*/ ansel =0x00;/*make the pins as digital i/o*/ trisa =0x20;/*according to the port a*/ trisc =0x80;/*enabling data out through SDO*/ sspstat |=0xc0;/*set the sample and phase*/ sspcon =0x30;/*enable the serial port bits and master mode fosc/4*/ intcon |=0xc0;/*enable all interrupts*/ pie1 |=0x08;/*enable peripheral interrupt*/ pir1 &=0xf7;/*reset peripheral interrupt flag*/ sspbuf =0x89; sspbuf =0x0a;/*put the data to be sent */ port_a &= 0xFE;/*led off*/ delay_ms(200);/*delay 1 sec*/ while(sspstat & 0x01);/*comparison for the int flag bit to be 1 if true go in*/ port_a |= 0x01;/*led glow*/ delay_ms(200);/*delay 1 sec*/ port_a &= 0xFE;/*led off*/ delay_ms(200);/*delay 1 sec*/ pir1 &= 0xf7;/*make the flag bit zero again*/ if(sspbuf !=0x0a) { port_a |= 0x01; delay_ms(1000); port_a &= 0xFE; delay_ms(1000); } }Please tell me what is the fault with this code??
Comments: