Author Topic: PIC Beginner  (Read 4288 times)

0 Members and 1 Guest are viewing this topic.

Offline MrAureliusRTopic starter

  • Supporter
  • ****
  • Posts: 373
  • Country: ca
PIC Beginner
« on: June 02, 2014, 01:39:56 am »
Hey everyone. I've been using AVR and TI micros for a while now, but for various reasons I need to try and build a (fairly) simple project using a PIC. I have several PICs at hand, and I've chosen the PIC18F4685 as it seems to have everything I need for this project.

I'm not at all familiar with the PIC registers (I'm programming in C). I've figured out the basics so far, like the data direction registers are TRISx; the inputs are read from PORTx; and the outputs go to LATx.

So I think I can handle GPIO. My question is regarding SPI. I need to communicate with a MAX7219, which uses SPI. I know the SPI protocol well, and I have used this chip before successfully. I just need to translate that into PIC C.

So, my two most burning questions are: how do I set the config fuses inside the PIC to use the internal oscillator, with all the standard options? (ie, serial programming enabled, watchdog off, brownout detect off, lock bits off, and so forth). I just need to understand the basic fuse configuration procedure and I can take it from there. I've seen various approaches online, some using #pragma config, some using __CONFIG(), not sure which is supposed to be used with the 18F4685, and even what the options are. The fuses shown in the MiniPro programmer software are overwhelming (at least compared to AVR) and I'm not sure what each do. I'm slowly sifting through the datasheet for the 18F4685, and it definitely has a lot of good info, but it doesn't have any C programming examples!

The other is how to get SPI up and running properly. Is there an spi.h somewhere? Or do I just control it through registers, like in my code below?
Also, I think I've figured out how to initialize the SPI as master on the 18F4685... this is what I have so far

Code: [Select]
/*
 * File:   main.c
 * Author: Max
 *
 * Created on June 1, 2014, 8:55 PM
 */

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <pic18f4685.h>

// need to figure out how to configure the fuses
// within the code...
// we want INTIO2 for clock
#pragma

/*
 * the SPI module has four registers,
 * SSPCON1, SSPSTAT, SSPBUF, SSPSR
 * Control, Status,  I/O Buffer, not accessible
 *
 */

void setupSPI(void)
{
    SSPSTAT = 0x00; // all bits need to be 0 here
    SSPCON1 = 0x21; // set up SPI as master, clock is Fosc/16
}
void setupMAX(void)
{
    // MAX7219 init code to go here
}
uint8_t getBuses(void)
{
    // this function will read the address and data buses, and send the results to the MAX7219 via SPI

    uint8_t addrH = 0x00;
    uint8_t addrL = 0x00;
    uint8_t data = 0x00;
    uint8_t addrH_prev;
    uint8_t addrL_prev;
    uint8_t data_prev;
   
    addrH = PORTB; // still not quite sure how I'm going to juggle the input of the buses around the pins I have available.
    addrL = PORTD; // the problem is that the SPI is right in the middle of port C, and /SS is in the middle of port A. I guess I'll have to mask those bits, and do some shifting and or'ing.
}
void main(void) {

    // set up ports
    TRISB = 0xFF; // PORTB all inputs
    TRISA = 0xDF; // PORTA RA5 is output, as /SS
    TRISC = 0xD7; // PORTC SDO, SCK outputs
    TRISD = 0xFF; // PORTD all inputs
   
    setupSPI();
   
    while(1){
       
    }   
}

I am a pretty quick learner, I just need to get it sending SPI bytes and I can handle the rest (I think). I'm using MPLAX X IDE v2.00 with the XC8 compiler. I've got an imitation PicKit 3 (which has worked fine the couple times I've had to burn .hex files to PICs). I also have my MiniPro programmer, so I'm not worried about programming the thing. Does the PicKit have debugging built in?

Any and all help is greatly, and I mean greatly appreciated!!

EDIT: I just tried to build, and it seems the compiler doesn't know what to do with uint8_t as a type... not sure what to do about that? Just use int? Or does the compiler assume that int is 16 bits? I read somewhere that the PIC compilers treat different types in different ways.... how confusing!
« Last Edit: June 02, 2014, 01:42:50 am by MrAureliusR »
--------------------------------------
Canadian hacker
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2863
  • Country: au
Re: PIC Beginner
« Reply #1 on: June 02, 2014, 02:23:23 am »
EDIT: I just tried to build, and it seems the compiler doesn't know what to do with uint8_t as a type... not sure what to do about that

uinit8_t is valid in xc8 (MPLABX).
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5144
  • Country: ro
  • .
Re: PIC Beginner
« Reply #2 on: June 02, 2014, 02:51:31 am »
Add 

#include <stdint.h>

after  #include <xc.h>

The uint8_t is defined in stdint.h  in XC8 (see <install path>\xc8\v1.31\docs\MPLAB_XC8_C_Compiler_User_Guide.pdf  page 21)

ps. alternatively, do a search and replace and replace uint8_t with "unsigned char" (though char is the same as unsigned char in xc8) ... in xc8 , char is an unsigned byte, just what uint8_t is supposed to be.

<install path>\xc8\v1.31\include\stdint.h:
Quote
#ifndef uint8_t
typedef unsigned char uint8_t;
#define uint8_t uint8_t
#define UINT8_MAX (255)
#endif
« Last Edit: June 02, 2014, 02:54:40 am by mariush »
 

Offline MrAureliusRTopic starter

  • Supporter
  • ****
  • Posts: 373
  • Country: ca
Re: PIC Beginner
« Reply #3 on: June 02, 2014, 03:57:02 am »
Add 

#include <stdint.h>

after  #include <xc.h>

The uint8_t is defined in stdint.h  in XC8 (see <install path>\xc8\v1.31\docs\MPLAB_XC8_C_Compiler_User_Guide.pdf  page 21)

ps. alternatively, do a search and replace and replace uint8_t with "unsigned char" (though char is the same as unsigned char in xc8) ... in xc8 , char is an unsigned byte, just what uint8_t is supposed to be.

<install path>\xc8\v1.31\include\stdint.h:
Quote
#ifndef uint8_t
typedef unsigned char uint8_t;
#define uint8_t uint8_t
#define UINT8_MAX (255)
#endif

Thanks for the tip mariush! That worked.

Anyone have any info on the config bits? I'm still looking for something relating specifically to the 4685.
--------------------------------------
Canadian hacker
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5144
  • Country: ro
  • .
Re: PIC Beginner
« Reply #4 on: June 02, 2014, 04:14:30 am »
Datasheet for that PIC has a chapter where all those config bits are explained.

Also, once you select the microcontroller in your project properties  in mplabx , you can go to window > pic memory views > configuration bits ...select what you want then click generate source code to output .   copy that into your project after the #include lines.
 

Offline MrAureliusRTopic starter

  • Supporter
  • ****
  • Posts: 373
  • Country: ca
Re: PIC Beginner
« Reply #5 on: June 02, 2014, 07:13:09 am »
Awesome, thanks for the help.

It seems my switch to PIC was a good one. I have it sending the SPI data perfectly. Still some kinks to iron out, but it's working a hell of a lot better than the AVR was.

Switching to PIC was much easier than I was expecting. I guess my earlier statement that C is C is C turned out to be true. Everything has a sort of familiarity to it, even though all the terms are different, things are still the same.

Here's hoping I can get everything working when I get up tomorrow!
--------------------------------------
Canadian hacker
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf