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

Header file for 16F876 / 16F877 with unions and structs

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

Header file for 16F876 / 16F877 with unions and structs
PostPosted: Tue Aug 10, 2004 8:04 am     Reply with quote

This just makes things a bit easier
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 10, 2004 8:04 am     Reply with quote

I had to remove the unnamed stuct used in unions. CCS doesn't like them.

Code:

#ifndef 16F876_H
#define 16F876_H

//////// Standard Header file for the PIC16F876 device ////////////////
#device PIC16F876
#nolist
//////// Program memory: 8192x14  Data RAM: 367  Stack: 8
//////// I/O: 22   Analog Pins: 5
//////// Data EEPROM: 256
//////// C Scratch area: 77   ID Location: 2000
//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,NOPUT,PUT,PROTECT,PROTECT_5%
//////// Fuses: PROTECT_50%,NOPROTECT,NOBROWNOUT,BROWNOUT,LVP,NOLVP,CPD
//////// Fuses: NOCPD,WRT,NOWRT,DEBUG,NODEBUG
////////
////////////////////////////////////////////////////////////////// I/O
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),
//                         PORT_B_PULLUPS(), INPUT(),
//                         OUTPUT_LOW(), OUTPUT_HIGH(),
//                         OUTPUT_FLOAT(), OUTPUT_BIT()
// Constants used to identify pins in the above are:

#define PIN_A0  40
#define PIN_A1  41
#define PIN_A2  42
#define PIN_A3  43
#define PIN_A4  44
#define PIN_A5  45

#define PIN_B0  48
#define PIN_B1  49
#define PIN_B2  50
#define PIN_B3  51
#define PIN_B4  52
#define PIN_B5  53
#define PIN_B6  54
#define PIN_B7  55

#define PIN_C0  56
#define PIN_C1  57
#define PIN_C2  58
#define PIN_C3  59
#define PIN_C4  60
#define PIN_C5  61
#define PIN_C6  62
#define PIN_C7  63

/********************************************************** Useful defines
   Function Registers
***************************************************************************/
unsigned char ADCON0;
#locate ADCON0=0x1F

struct {
    unsigned char ADON:1;
    unsigned char UNUSED0:1;
    unsigned char GO_DONE:1;
    unsigned char CHS0:1;
    unsigned char CHS1:1;
    unsigned char CHS2:1;
    unsigned char ADCS0:1;
    unsigned char ADCS1:1;
} ADCON0bits;
#locate ADCON0bits=0x1F

unsigned char ADCON1;
#locate ADCON1=0x9F

struct {
    unsigned char PCFG0:1;
    unsigned char PCFG1:1;
    unsigned char PCFG2:1;
    unsigned char PCFG3:1;
    unsigned char UNUSED:3;
    unsigned char ADFM:1;
} ADCON1bits ;
#locate ADCON1bits=0x9F

unsigned char ADRESH;
#locate ADRESH=0x1E

unsigned char ADRESL;
#locate ADRESL=0x9E

unsigned char CCP1CON;
#locate CCP1CON=0x17

struct {
    unsigned char CCP1M0:1;
    unsigned char CCP1M1:1;
    unsigned char CCP1M2:1;
    unsigned char CCP1M3:1;
    unsigned char CCP1Y:1;
    unsigned char CCP1X:1;
} CCP1CONbits ;
#locate CCP1CONbits=0x17

unsigned char CCP2CON;
#locate CCP2CON=0x1D

struct {
    unsigned char CCP2M0:1;
    unsigned char CCP2M1:1;
    unsigned char CCP2M2:1;
    unsigned char CCP2M3:1;
    unsigned char CCP2Y:1;
    unsigned char CCP2X:1;
} CCP2CONbits ;
#locate CCP2CONbits=0x1D

unsigned int16 CCPR1;
#locate CCPR1=0x15

unsigned char CCPR1H;
#locate CCPR1H=0x16

unsigned char CCPR1L;
#locate CCPR1L=0x15

unsigned int16 CCPR2;
#locate CCPR2=0x1B

unsigned char CCPR2H;
#locate CCPR2H=0x1C

unsigned char CCPR2L;
#locate CCPR2L=0x1B

unsigned char EEADR;
#locate EEADR=0x10D

unsigned char EECON1;
#locate EECON1=0x18C
struct {
    unsigned char RD:1;
    unsigned char WR:1;
    unsigned char WREN:1;
    unsigned char WRERR:1;
    unsigned char UNUSED:3;
    unsigned char EEPGD:1;
} EECON1bits ;
#locate EECON1bits=0x18C

unsigned char EECON2;
#locate EECON2=0x18D

unsigned char EEDATA;
#locate EEDATA=0x10C

unsigned char EEDATH;
#locate EEDATA=0x10E

unsigned char EEADRH;
#locate EEDATA=0x10F

unsigned char FSR;
#locate FSR=0x04

unsigned char INDF;
#locate INDF=0x00

unsigned char INTCON;
#locate INTCON=0x0B

struct
{
  unsigned char RBIF:1;
  unsigned char INTF:1;
  unsigned char T0IF:1;
  unsigned char RBIE:1;
  unsigned char INTE:1;
  unsigned char T0IE:1;
  unsigned char PEIE:1;
  unsigned char GIE:1;
} INTCONbits;
#locate INTCONbits=0x0B

unsigned char OPTION;
#locate OPTION=0x81

struct
{
  unsigned char PS0:1;
  unsigned char PS1:1;
  unsigned char PS2:1;
  unsigned char PSA:1;
  unsigned char T0SE:1;
  unsigned char T0CS:1;
  unsigned char INTEDG:1;
  unsigned char NOT_RBPU:1;
} OPTIONbits;
#locate OPTIONbits=0x81

unsigned char PCL;
#locate RCON=0x02

unsigned char PCLATH;
#locate RCON=0x0A

unsigned char PCON;
#locate RCON=0x8E

struct
{
  unsigned char NOT_BOR:1;
  unsigned char NOT_POR:1;
} PCONbits ;
#locate PCONbits=0x8E

unsigned char PIE1;
#locate PIE1=0x8C

struct {
    unsigned char TMR1IE:1;
    unsigned char TMR2IE:1;
    unsigned char CCP1IE:1;
    unsigned char SSPIE:1;
    unsigned char TXIE:1;
    unsigned char RCIE:1;
    unsigned char ADIE:1;
    unsigned char PSPIE:1;
} PIE1bits ;
#locate PIE1bits=0x8C

unsigned char PIE2;
#locate PIE2=0x8D

struct {
    unsigned char CCP2IE:1;
    unsigned char UNUSED0:2;
    unsigned char BCLIE:1;
    unsigned char EEIE:1;
} PIE2bits ;
#locate PIE2bits=0x8D

unsigned char PIR1;
#locate PIR1=0x0C

struct {
    unsigned char TMR1IF:1;
    unsigned char TMR2IF:1;
    unsigned char CCP1IF:1;
    unsigned char SSPIF:1;
    unsigned char TXIF:1;
    unsigned char RCIF:1;
    unsigned char ADIF:1;
    unsigned char PSPIF:1;
} PIR1bits ;
#locate PIR1bits=0x0C

unsigned char PIR2;
#locate PIR2=0x0D

struct {
    unsigned char CCP2IF:1;
    unsigned char UNUSED0:2;
    unsigned char BCLIF:1;
    unsigned char EEIF:1;
} PIR2bits ;
#locate PIR2bits=0x0D

unsigned char PORTA;
#locate PORTA=0x05

struct
{
  unsigned char RA0:1;
  unsigned char RA1:1;
  unsigned char RA2:1;
  unsigned char RA3:1;
  unsigned char RA4:1;
  unsigned char RA5:1;
  unsigned char RA6:1;
} PORTAbits ;
#locate PORTAbits=0x05

unsigned char PORTB;
#locate PORTB=0x06

struct
{
  unsigned char RB0:1;
  unsigned char RB1:1;
  unsigned char RB2:1;
  unsigned char RB3:1;
  unsigned char RB4:1;
  unsigned char RB5:1;
  unsigned char RB6:1;
  unsigned char RB7:1;
} PORTBbits ;
#locate PORTBbits=0x06

unsigned char PORTC;
#locate PORTC=0x07

struct
{
  unsigned char RC0:1;
  unsigned char RC1:1;
  unsigned char RC2:1;
  unsigned char RC3:1;
  unsigned char RC4:1;
  unsigned char RC5:1;
  unsigned char RC6:1;
  unsigned char RC7:1;
} PORTCbits ;
#locate PORTCbits=0x07

unsigned char PORTD;
#locate PORTD=0x08

struct
{
  unsigned char RD0:1;
  unsigned char RD1:1;
  unsigned char RD2:1;
  unsigned char RD3:1;
  unsigned char RD4:1;
  unsigned char RD5:1;
  unsigned char RD6:1;
  unsigned char RD7:1;
} PORTDbits ;
#locate PORTDbits=0x08

unsigned char PORTE;
#locate PORTE=0x09

struct
{
  unsigned char RE0:1;
  unsigned char RE1:1;
  unsigned char RE2:1;
} PORTEbits ;
#locate PORTEbits=0x09

unsigned char PR2;
#locate PR2=0x92

unsigned char RCREG;
#locate RCREG=0x1A

unsigned char RCSTA;
#locate RCSTA=0x18

struct {
    unsigned char RX9D:1;
    unsigned char OERR:1;
    unsigned char FERR:1;
    unsigned char ADDEN:1;
    unsigned char CREN:1;
    unsigned char SREN:1;
    unsigned char RX9:1;
    unsigned char SPEN:1;
} RCSTAbits ;
#locate RCSTAbits=0x18

unsigned char SPBRG;
#locate SPBRG=0x99

unsigned char SSPADD;
#locate SSPADD=0x93

unsigned char SSPBUF;
#locate SSPBUF=0x13

unsigned char SSPCON;
#locate SSPCON=0x14

struct {
    unsigned char SSPM0:1;
    unsigned char SSPM1:1;
    unsigned char SSPM2:1;
    unsigned char SSPM3:1;
    unsigned char CKP:1;
    unsigned char SSPEN:1;
    unsigned char SSPOV:1;
    unsigned char WCOL:1;
} SSPCONbits ;
#locate SSPCONbits=0x14

unsigned char SSPCON2;
#locate SSPCON2=0x91

struct {
    unsigned char SEN:1;
    unsigned char RSEN:1;
    unsigned char PEN:1;
    unsigned char RCEN:1;
    unsigned char ACKEN:1;
    unsigned char ACKDT:1;
    unsigned char ACKSTAT:1;
    unsigned char GCEN:1;
} SSPCON2bits ;
#locate SSPCON2bits=0x91

unsigned char SSPSTAT;
#locate SSPSTAT=0x94

struct {
    unsigned char BF:1;
    unsigned char UA:1;
    unsigned char R_W:1;
    unsigned char S:1;
    unsigned char P:1;
    unsigned char D_A:1;
    unsigned char CKE:1;
    unsigned char SMP:1;
} SSPSTATbits ;
#locate SSPSTATbits=0x94

unsigned char STATUS;
#locate STATUS=0x03

struct {
    unsigned char C:1;
    unsigned char DC:1;
    unsigned char Z:1;
    unsigned char NOT_PD:1;
    unsigned char NOT_TO:1;
    unsigned char RP0:1;
    unsigned char RP1:1;
    unsigned char IRP:1;
} STATUSbits ;
#locate STATUSbits=0x03

unsigned char T1CON;
#locate T1CON=0x10

struct
{
  unsigned char TMR1ON:1;
  unsigned char TMR1CS:1;
  unsigned char NOT_T1SYNC:1;
  unsigned char T1OSCEN:1;
  unsigned char T1CKPS0:1;
  unsigned char T1CKPS1:1;
} T1CONbits ;
#locate T1CONbits=0x10

unsigned char T2CON;
#locate T2CON=0x12

struct {
    unsigned char T2CKPS0:1;
    unsigned char T2CKPS1:1;
    unsigned char TMR2ON:1;
    unsigned char TOUTPS0:1;
    unsigned char TOUTPS1:1;
    unsigned char TOUTPS2:1;
    unsigned char TOUTPS3:1;
} T2CONbits ;
#locate T2CONbits=0x12

unsigned char TMR0;
#locate TMR0=0x01

unsigned int16 TMR1;
#locate TMR1=0x0E

unsigned char TMR1H;
#locate TMR1H=0x0F

unsigned char TMR1L;
#locate TMR1L=0x0E

unsigned char TMR2;
#locate TMR2=0x11

unsigned char TRISA;
#locate TRISA=0x85

struct {
    unsigned char TRISA0:1;
    unsigned char TRISA1:1;
    unsigned char TRISA2:1;
    unsigned char TRISA3:1;
    unsigned char TRISA4:1;
    unsigned char TRISA5:1;
} TRISAbits ;
#locate TRISAbits=0x85

unsigned char TRISB;
#locate TRISB=0x86

struct {
    unsigned char TRISB0:1;
    unsigned char TRISB1:1;
    unsigned char TRISB2:1;
    unsigned char TRISB3:1;
    unsigned char TRISB4:1;
    unsigned char TRISB5:1;
    unsigned char TRISB6:1;
    unsigned char TRISB7:1;
} TRISBbits ;
#locate TRISBbits=0x86

unsigned char TRISC;
#locate TRISC=0x87

struct {
    unsigned char TRISC0:1;
    unsigned char TRISC1:1;
    unsigned char TRISC2:1;
    unsigned char TRISC3:1;
    unsigned char TRISC4:1;
    unsigned char TRISC5:1;
    unsigned char TRISC6:1;
    unsigned char TRISC7:1;
} TRISCbits ;
#locate TRISCbits=0x87

unsigned char TRISD;
#locate TRISD=0x88

struct {
    unsigned char TRISD0:1;
    unsigned char TRISD1:1;
    unsigned char TRISD2:1;
    unsigned char TRISD3:1;
    unsigned char TRISD4:1;
    unsigned char TRISD5:1;
    unsigned char TRISD6:1;
    unsigned char TRISD7:1;
} TRISDbits ;
#locate TRISDbits=0x88

unsigned char TRISE;
#locate TRISE=0x89

struct {
    unsigned char TRISE0:1;
    unsigned char TRISE1:1;
    unsigned char TRISE2:1;
    unsigned char UNUSED0:1;
    unsigned char PSPMODE:1;
    unsigned char IBOV:1;
    unsigned char OBF:1;
    unsigned char IBF:1;
} TRISEbits ;
#locate TRISEbits=0x89

unsigned char TXREG;
#locate TXREG=0x19

unsigned char TXSTA;
#locate TXSTA=0x98

struct {
    unsigned char TX9D:1;
    unsigned char TRMT:1;
    unsigned char BRGH:1;
    unsigned char UNUSED:1;
    unsigned char SYNC:1;
    unsigned char TXEN:1;
    unsigned char TX9:1;
    unsigned char CSRC:1;
} TXSTAbits ;
#locate TXSTAbits=0x98


////////////////////////////////////////////////////////////////// Useful defines
#define FALSE 0
#define TRUE 1

#define BYTE int
#define BOOLEAN short int

#define getc getch
#define fgetc getch
#define getchar getch
#define putc putchar
#define fputc putchar
#define fgets gets
#define fputs puts

////////////////////////////////////////////////////////////////// Control
// Control Functions:  RESET_CPU(), SLEEP(), RESTART_CAUSE()
// Constants returned from RESTART_CAUSE() are:
#define WDT_FROM_SLEEP  0     
#define WDT_TIMEOUT     8     
#define MCLR_FROM_SLEEP 16   
#define NORMAL_POWER_UP 24   


////////////////////////////////////////////////////////////////// Timer 0
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER0(),
//                              SET_TIMER0() or SET_RTCC(),
//                              GET_TIMER0() or GET_RTCC()
// Constants used for SETUP_TIMER0() are:
#define RTCC_INTERNAL   0
#define RTCC_EXT_L_TO_H 32
#define RTCC_EXT_H_TO_L 48

#define RTCC_DIV_1      8
#define RTCC_DIV_2      0
#define RTCC_DIV_4      1
#define RTCC_DIV_8      2
#define RTCC_DIV_16     3
#define RTCC_DIV_32     4
#define RTCC_DIV_64     5
#define RTCC_DIV_128    6
#define RTCC_DIV_256    7


#define RTCC_8_BIT      0     

// Constants used for SETUP_COUNTERS() are the above
// constants for the 1st param and the following for
// the 2nd param:

////////////////////////////////////////////////////////////////// WDT
// Watch Dog Timer Functions: SETUP_WDT() or SETUP_COUNTERS() (see above)
//                            RESTART_WDT()
//
#define WDT_18MS        8   
#define WDT_36MS        9   
#define WDT_72MS       10   
#define WDT_144MS      11   
#define WDT_288MS      12   
#define WDT_576MS      13   
#define WDT_1152MS     14   
#define WDT_2304MS     15   

////////////////////////////////////////////////////////////////// Timer 1
// Timer 1 Functions: SETUP_TIMER_1, GET_TIMER1, SET_TIMER1
// Constants used for SETUP_TIMER_1() are:
//      (or (via |) together constants from each group)
#define T1_DISABLED         0
#define T1_INTERNAL         0x85
#define T1_EXTERNAL         0x87
#define T1_EXTERNAL_SYNC    0x83

#define T1_CLK_OUT          8

#define T1_DIV_BY_1         0
#define T1_DIV_BY_2         0x10
#define T1_DIV_BY_4         0x20
#define T1_DIV_BY_8         0x30

////////////////////////////////////////////////////////////////// Timer 2
// Timer 2 Functions: SETUP_TIMER_2, GET_TIMER2, SET_TIMER2
// Constants used for SETUP_TIMER_2() are:
#define T2_DISABLED         0
#define T2_DIV_BY_1         4
#define T2_DIV_BY_4         5
#define T2_DIV_BY_16        6

////////////////////////////////////////////////////////////////// CCP
// CCP Functions: SETUP_CCPx, SET_PWMx_DUTY
// CCP Variables: CCP_x, CCP_x_LOW, CCP_x_HIGH
// Constants used for SETUP_CCPx() are:
#define CCP_OFF                         0
#define CCP_CAPTURE_FE                  4
#define CCP_CAPTURE_RE                  5
#define CCP_CAPTURE_DIV_4               6
#define CCP_CAPTURE_DIV_16              7
#define CCP_COMPARE_SET_ON_MATCH        8
#define CCP_COMPARE_CLR_ON_MATCH        9
#define CCP_COMPARE_INT                 0xA
#define CCP_COMPARE_RESET_TIMER         0xB
#define CCP_PWM                         0xC
#define CCP_PWM_PLUS_1                  0x1c
#define CCP_PWM_PLUS_2                  0x2c
#define CCP_PWM_PLUS_3                  0x3c
long CCP_1;
#byte   CCP_1    =                      0x15       
#byte   CCP_1_LOW=                      0x15       
#byte   CCP_1_HIGH=                     0x16       
long CCP_2;
#byte   CCP_2    =                      0x1B       
#byte   CCP_2_LOW=                      0x1B       
#byte   CCP_2_HIGH=                     0x1C       
////////////////////////////////////////////////////////////////// SPI
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN
// Constants used in SETUP_SSP() are:
#define SPI_MASTER       0x20
#define SPI_SLAVE        0x24
#define SPI_L_TO_H       0
#define SPI_H_TO_L       0x10
#define SPI_CLK_DIV_4    0
#define SPI_CLK_DIV_16   1
#define SPI_CLK_DIV_64   2
#define SPI_CLK_T2       3
#define SPI_SS_DISABLED  1

#define SPI_SAMPLE_AT_END 0x8000
#define SPI_XMIT_L_TO_H  0x4000

////////////////////////////////////////////////////////////////// UART
// Constants used in setup_uart() are:
// FALSE - Turn UART off
// TRUE  - Turn UART on
#define UART_ADDRESS 2
#define UART_DATA    4
// TRUE  - Turn UART on

////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
//                SET_ADC_CHANNEL(), READ_ADC()
// Constants used for SETUP_ADC() are:
#define ADC_OFF                0          // ADC Off
#define ADC_CLOCK_DIV_2    0x100
#define ADC_CLOCK_DIV_8     0x40
#define ADC_CLOCK_DIV_32    0x80
#define ADC_CLOCK_INTERNAL  0xc0          // Internal 2-6us

// Constants used in SETUP_ADC_PORTS() are:
#define NO_ANALOGS                           7    // None
#define ALL_ANALOG                           0    // A0 A1 A2 A3 A5         
#define AN0_AN1_AN2_AN4_VSS_VREF             1    // A0 A1 A2 A5 VRefh=A3             
#define AN0_AN1_AN3                          4    // A0 A1 A3
#define AN0_AN1_VSS_VREF                     5    // A0 A1 VRefh=A3
#define AN0_AN1_AN4_VREF_VREF             0x08    // A0 A1 A5 VRefh=A3 VRefl=A2             
#define AN0_AN1_VREF_VREF                 0x0D    // A0 A1 VRefh=A3 VRefl=A2
#define AN0                               0x0E    // A0
#define AN0_VREF_VREF                     0x0F    // A0 VRefh=A3 VRefl=A2
#define ANALOG_RA3_REF         0x1         //!old only provided for compatibility
#define RA0_RA1_RA3_ANALOG     0x4         //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_REF 0x5         //!old only provided for compatibility
#define ANALOG_RA3_RA2_REF              0x8   //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_RA2_REF      0xD   //!old only provided for compatibility
#define RA0_ANALOG                      0xE   //!old only provided for compatibility
#define RA0_ANALOG_RA3_RA2_REF          0xF   //!old only provided for compatibility


// Constants used in READ_ADC() are:
#define ADC_START_AND_READ     7   // This is the default if nothing is specified
#define ADC_START_ONLY         1
#define ADC_READ_ONLY          6



////////////////////////////////////////////////////////////////// INT
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
//                      EXT_INT_EDGE()
//
// Constants used in EXT_INT_EDGE() are:
#define L_TO_H              0x40
#define H_TO_L                 0
// Constants used in ENABLE/DISABLE_INTERRUPTS() are:
#define GLOBAL                    0x0BC0
#define INT_RTCC                  0x0B20
#define INT_RB                    0x0B08
#define INT_EXT                   0x0B10
#define INT_AD                    0x8C40
#define INT_TBE                   0x8C10
#define INT_RDA                   0x8C20
#define INT_TIMER1                0x8C01
#define INT_TIMER2                0x8C02
#define INT_CCP1                  0x8C04
#define INT_CCP2                  0x8D01
#define INT_SSP                   0x8C08
#define INT_BUSCOL                0x8D08
#define INT_EEPROM                0x8D10
#define INT_TIMER0                0x0B20

#list
#endif  /* 16F876_H */
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 11, 2004 6:03 am     Reply with quote

Edited the above code.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
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