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
/*
* 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!