Jump to content

Netlab

EstablishedMember
  • Content Count

    5
  • Joined

  • Last visited

Community Reputation

0 Neutral

About Netlab

  • Rank
    Newbrie
  1. hmmm, I see where you're coming from. I was under the impression that the I2C interrupt flag would occur whenever data was received. But I think you're right it doesn't make sense for it to be there. I'll get rid of the line and see if I have any more luck. Thanks for the help, I guess I just didn't look at the code logically since I've been staring at it for days Mike
  2. Doesn't anyone have any ideas? I'm no closer to solving this problem. Please help
  3. Hi I am having trouble getting my 16f819 to read data from an existing I2C bus. I'm confident that my SSP setup is correct, but when I try to read the data being transmitted the PIC seems to crash/hang. The master on the I2C bus is only transmitting data. There is no facility for feedback to the master (apart from the I2C ack signals). Am I making any obvious mistakes? My code is included below ////////////////////////////////////////////////////////////////////////////////////////////// // Michael Eaton, Adrian Brown and John Duffy // ////////////////////////////////////////////////////////////////////////////////////////////// // This code is controlling a slave I2C device. It waits until it is addressed and then // // receives 5 bytes of data. The first byte is the address and the following four are // // to be stored in variables called data1 to data4. // ////////////////////////////////////////////////////////////////////////////////////////////// #include <system.h> #include <i2c_driver.h> #pragma CLOCK_FREQ 8000000 //config clock to 8mhz. #pragma DATA _CONFIG, _INTRC_IO & _WDT_OFF & _CP_OFF & _PWRTE_OFF &_LVP_OFF unsigned char data0 = 0; //variables for storing the data received unsigned char data1 = 0; //via I2C. unsigned char data2 = 0; //unsigned char = 8bits unsigned char data3 = 0; unsigned char data4 = 0; void main() { osccon = 0x74; //Sets internal oscillator stable bit and internal frequancey to 8MHz intcon=0b11000000; //set GIE and PEIE interrupts. pie1=0x08; //sets SSPIE on and other interrupts off in this register. trisb.1=1; //set portb pin 1 as input for I2C reception trisb.4=1; //set portb pin 4 as input for I2C reception sspadd = 0x50; //set address as 0101 0000 (address 8 on device config)(0100 000 + 8 + R/W bit) sspstat=0x00; //clears all sspstat bits. sspcon=0x36; //sets ssp register to 00110110 (36hex) for I2C adcon1 = 0x06; //Sets all A/D ports to digital trisa=0; //set porta as outputs porta=0; //set porta to zero, because they initialize to ON automatically while (1) //loop infinitely { porta.1=1; //turn on green led delay_ms(100); porta.1=0; //turn off green led delay_ms(100); } } void interrupt() //The interrupt routine. This starts when I2C comms is detected. { intcon=0x00; //disable all interrupts (GIE and PIE) data0=sspbuf; //transfer i2c buffer to data0 and clear the buffer full bit pir1.SSPIF=0; //clear the interrupt flag sspcon.SSPOV=0; //clear the overflow bit while (sspstat.BF==0); //loop here while data is being received. Waits for the Buffer Full bit. //Data byte 1 data1=sspbuf; //save i2c buffer to data1 and clear the buffer full bit while (pir1.SSPIF==0); //loop here until I2C interrupt occurs. pir1.SSPIF=0; //clear the interrupt flag sspcon.SSPOV=0; //clear the overflow bit //Data byte 2 while (sspstat.BF==0); //loop here while data is being received. Waits for the Buffer Full bit. data2=sspbuf; //save i2c buffer to data2 and clear the buffer full bit while (pir1.SSPIF==0); //loop here until I2C interrupt occurs. pir1.SSPIF=0; //clear the interrupt flag sspcon.SSPOV=0; //clear the overflow bit //Data byte 3 while (sspstat.BF==0); //loop here while data is being received. Waits for the Buffer Full bit. data3=sspbuf; //save i2c buffer to data3 and clear the buffer full bit while (pir1.SSPIF==0); //loop here until I2C interrupt occurs. pir1.SSPIF=0; //clear the interrupt flag sspcon.SSPOV=0; //clear the overflow bit //Data byte 4 while (sspstat.BF==0); //loop here while data is being received. Waits for the Buffer Full bit. data4=sspbuf; //save i2c buffer to data4 and clear the buffer full bit while (pir1.SSPIF==0); //loop here until I2C interrupt occurs. pir1.SSPIF=0; //clear the interrupt flag sspcon.SSPOV=0; //clear the overflow bit intcon=0b11000000; //enable GIE and PIE interrupts. } Thanks Mike
  4. WOW!! Not really sure why this works....but it is working beautifully. I just added the _LVP_OFF like you say and its all good. I'm using a programmer supplied by a 9V plug pack. Does this make sense to use _LVP_OFF? Thanks a lot Reynard.
  5. Hi, I am currently trying to get a PIC16f819 to flash two LEDs on and off, with the internal oscilator set at 8MHz. I'm using the code at the bottom of this post, the original code found at the link below - which uses an external oscilator set at 4MHz: http://forum.sourceboost.com/index.php?sho...mp;hl=beginner), However, it is not working properly, some random events are happening. One LED will come on, then if I touch any metal part of the circuit both LEDS start to flash correctly. I've made my own circuit board to eliminate the option of a dodgey bread board, and put a Cap across the power supply. If someone could give some advice it would be greatly appreciated. #include <system.h> #pragma CLOCK_FREQ 8000000 // config clock to 8mhz. #pragma DATA _CONFIG, _INTRC_IO & _WDT_OFF & _CP_OFF & _PWRTE_OFF void main() { osccon = 0x74; //setting oscilator to 8MHz trisb = 0; //configure port B pins as output while( 1 ) //endless loop { portb.0=1; // set portb pin0 to high. portb.1=0; // set portb pin1 to low. delay_ms(50); // pause 0.5 seconds portb=0x02; // shows hex notation for entering data. delay_ms(50); // you can also write portb=2 in decimal } }
×
×
  • Create New...