CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C Communication Problem using PIC16F877A
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
electric2277
Guest







HMC6352 with PIC18LF4321
PostPosted: Thu Feb 18, 2010 5:27 pm     Reply with quote

I am trying to get my HMC6352 working with my PIC18LF4321, however when I use the code at the beginning of this post (slightly tweaked), I get very large number for the msb and lsb (the number is 4294836352).

Something to note is that I could not compile with
"lsb = SWReadI2C(0);"

I had to leave the '0' out in order to not have an "error [1204] too many arguments in function call"

My code is below:
Code:

#include "p18f4321.h"           // Define PIC18LF4321 registers and bits
#include "usart.h"            // Define Usart settings for serial output
#include "delays.h"            // def
#include <stdio.h>
#include "sw_i2c.h"

/*******************************
 * Configuration selections
 *******************************
 */

#pragma config OSC = XT         // Use RA6 and RA7 for 1-4MHz crystal oscillator
#pragma config WDT = OFF        // Disable watchdog timer
#pragma config PWRT = OFF       // Disable power-up delay             //
#pragma config LVP = OFF        // Disable low-voltage programming       //
#pragma config MCLRE = ON       // Enable master clear pin //
#pragma config CCP2MX = RB3     // Connect CCP2 internally to RB3 pin //
#pragma config BOR = SOFT       // Brown-out reset controlled by software //
#pragma config BORV = 3         // Brown-out voltage set for 2.1V, nominal //
#pragma config LPT1OSC = OFF    // Deselect low-power Timer1 oscillator //

/*******************************
 * Global variables
 *******************************/

#define HMC6352_I2C_WRITE_ADDRESS 0x42
#define HMC6352_I2C_READ_ADDRESS  0x43
#define HMC6352_GET_DATA_COMMAND 0x41

/*******************************
 * Function prototypes
 *******************************
 */

void Initial(void);
void InitTX(void);
void InitI2C(void);
void HMC6352_read_heading(void);


void HMC6352_read_heading()
{
   int lsb;
   int msb;
   int addy;

   SWStartI2C();                              //i2c_start();
   SWWriteI2C(HMC6352_I2C_WRITE_ADDRESS);      //i2c_write(HMC6352_I2C_WRITE_ADDRESS);
   SWWriteI2C(HMC6352_GET_DATA_COMMAND);         //i2c_write(HMC6352_GET_DATA_COMMAND);
   SWStopI2C();                           //i2c_stop();
   
   Delay1KTCYx(40);    //delays 4000 cycles of 4 MHZ = 1 ms   //delay_ms(7);  // Allow 1 ms extra for safety

   
   SWStartI2C();                        //i2c_start();
   SWWriteI2C(HMC6352_I2C_READ_ADDRESS);      //i2c_write(HMC6352_I2C_READ_ADDRESS);
   msb = SWReadI2C();                     //msb=i2c_read();
   lsb = SWReadI2C(0);                     //msb=i2c_read(0);
   SWStopI2C();                        //i2c_stop();
   
   printf("MSB = %lu\n\r", msb);
   printf("LSB = %lu\n\r", lsb);

}

/////// Main program //////////////////////////////////////////////////////////

/*******************************
 * main
 *******************************
 */


void main()
{
    Initial();                  // Initialize everything
   InitTX();               // Initialize the UART
   printf("Compass Heading = ");

   while (1)
   {
     HMC6352_read_heading();
   Delay10KTCYx(100);
   }
}

/*******************************
 * Initial
 *
 * This function performs all initializations of variables and registers.
 *******************************
 */

void Initial()
{
   OSCCON = 0b01100010;         // Use Fosc = 4 MHz (Fcpu = 1 MHz)
   ADCON1 = 0b00001011;         // RA0,RA1,RA3 pins analog; others digital
   TRISA = 0b00001111;          // Set I/O for PORTA
   TRISB = 0b00010000;          // Set I/O for PORTB
   TRISC = 0b00001100;         // Set I/O for PORTC
   TRISD = 0b10000000;          // Set I/O for PORTD
   TRISE = 0b00000010;          // Set I/O for PORTE
   PORTA = 0;                   // Set initial state for all outputs low
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;
   PORTE = 0;
   Delay10KTCYx(50);
   RCONbits.SBOREN = 0;         // Now disable brown-out reset
}


void InitTX()
{

RCSTA = 0b10010000; // Enable UART
TXSTA = 0b00100000; // Enable TX
SPBRG = 12; // Set baud rate  corresponds to 19200 baudrate

BAUDCON = 0b00001000;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 18, 2010 5:34 pm     Reply with quote

You're using the Microchip C18 compiler. This forum is for the CCS compiler.
electric2277
Guest







CCS Compiler
PostPosted: Fri Feb 19, 2010 10:39 am     Reply with quote

Where do I go to get the CCS Compiler. I am guessing that it is not free.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
Jump to:  
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