Questi sono gli errori:
- Codice: Seleziona tutto
Error [103] C:\Users\fanta\Desktop\delay.h; 47.39 #error: please define PIC_CLK correctly
Error [103] C:\Users\fanta\Desktop\delay.h; 101.39 #error: please define PIC_CLK correctly
Error [103] C:\Users\fanta\Desktop\delay.h; 177.39 #error: please define PIC_CLK correctly
Questi i file del progetto:
file.c
- Codice: Seleziona tutto
#include <PIC.h> // contiene i nomi mnemonici di registri e porte
__CONFIG(0x3FF9);
#define PIC_CLK 4000000 //4Mhz
#include "delay.c"
#include "settaggioservo.h" // file che contiene il settaggio delle porte
void main(void)
{
settaggio();
DelayUs(100);
while(a)
{
for (m=0;m<30;m++)
{
LED=1; // accendo LED
SER1=1; // segnale livello alto 1
for (i=0;i<6;i++)
{
DelayUs(250); //attesa 1.5 millisec. (per posizione centrale)
}
SER1=0; // segnale livello basso 0
for (i=0;i<74;i++)
{
DelayUs(250); // attesa 18,5 millisec.
}
}
LED=0; //spengo LED
a=0; //esco dal ciclo while(a)
}
while(1)
{
if(PL1==0) //se premo pulsante 1
{
LED=1; //accendo LED
SER1=1; // segnale livello alto 1
for (i=0;i<21;i++)
{
DelayUs(100); //attesa 2.1 millisec.
}
SER1=0; // segnale livello basso 0
for (i=0;i<180;i++)
{
DelayUs(99); //attesa 17.9 millisec. }
}
}
else
{
LED=0;
} //altimenti spengo LED
if(PL2==0) //se premo pulsante 2
{
LED=1; //accendo LED
SER1=1; // segnale livello alto 1
for (i=0;i<6;i++)
{
DelayUs(100); //attesa 0.6 millisec.
}
SER1=0; // segnale livello basso 0
for (i=0;i<194;i++)
{
DelayUs(100); // attesa 19.4 millisec.
}
}
else{LED=0;} //altrimenti spengo LED
if(PL3==0) //se premo pulsante 3
{
LED=1; // accendo LED
SER1=1; // segnale livello alto 1
for (i=0;i<6;i++)
{
DelayUs(250); //attesa 1.5 millisec. (per posizione centrale)
}
SER1=0; // segnale livello basso 0
for (i=0;i<74;i++)
{
DelayUs(250); // attesa 18,5 millisec.
}
}
else{LED=0;}
}
a=0;
}
delay.c
- Codice: Seleziona tutto
/*
high level delay routines - see delay.h for more info.
Designed by Shane Tolmie of www.microchipC.com corporation. Freely distributable.
Questions and comments to webmaster@microchipC.com.
PICuWEB - Program PIC micros with C. Site has FAQ and sample source code.
www.microchipC.com.
For Microchip 12C67x, 16C7x, 16F87x and Hi-Tech C
*/
#include "delay.h"
void DelayMs(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
void DelayBigMs(unsigned int cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
void DelayS(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayMs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
delay.h
- Codice: Seleziona tutto
/*
lowlevel delay routines
Designed by Shane Tolmie for www.microchipC.com. Freely distributable.
Questions and comments to webmaster@microchipC.com.
For Microchip 12C67x, 16C7x, 16F87x and Hi-Tech C
Example C:
#define PIC_CLK 4000000
#include "delay.h"
unsigned int timeout_int, timeout_char;
DelayUs(40); //do NOT do DelayUs(N) of N<5 @ 4Mhz or else it executes DelayUs(255) !!!!
DelayUs(255); //max
dly250n; //delay 250ns
dly1u; //delay 1us
timeout_char=timeout_char_us(1147);
while(timeout_char-- && (RA1==0)); //wait up to 1147us for port RA1 to go high
// - this is the max timeout
timeout_int=timeout_int_us(491512);
while(timeout_int-- && (RA1==0)); //wait up to 491512us for port RA1 to go high
// - this is the max timeout
*/
#ifndef __DELAY_H
#define __DELAY_H
#define PIC_CLK 4000000
extern unsigned char delayus_variable;
#if (PIC_CLK == 4000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n please remove; for 8Mhz+ only
#define dly1u asm("nop")
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 8000000)
#define dly125n please remove; for 32Mhz+ only
#define dly250n please remove; for 16Mhz+ only
#define dly500n asm("nop")
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif ( (PIC_CLK == 16000000) || (PIC_CLK == 16257000) )
#define dly125n please remove; for 32Mhz+ only
#define dly250n asm("nop")
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#elif (PIC_CLK == 20000000)
#define dly200n asm("nop")
#define dly400n dly250n;dly250n
#define dly2u dly400n;dly400n;dly400n;dly400n;dly400n
#elif (PIC_CLK == 32000000)
#define dly125n asm("nop")
#define dly250n dly125n;dly125n
#define dly500n dly250n;dly250n
#define dly1u dly500n;dly500n
#define dly2u dly1u;dly1u
#else
#error delay.h - please define pic_clk correctly
#endif
//*****
//delay routine
#if PIC_CLK == 4000000
#define DelayDivisor 4
#define WaitFor1Us asm("nop")
#define Jumpback asm("goto $ - 2")
#elif PIC_CLK == 8000000
#define DelayDivisor 2
#define WaitFor1Us asm("nop")
#define Jumpback asm("goto $ - 2")
#elif ( (PIC_CLK == 16000000) || (PIC_CLK==16257000) )
#define DelayDivisor 1
#define WaitFor1Us asm("nop")
#define Jumpback asm("goto $ - 2")
#elif PIC_CLK == 20000000
#define DelayDivisor 1
#define WaitFor1Us asm("nop"); asm("nop")
#define Jumpback asm("goto $ - 3")
#elif PIC_CLK == 32000000
#define DelayDivisor 1
#define WaitFor1Us asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop")
#define Jumpback asm("goto $ - 6")
#else
#error delay.h - please define pic_clk correctly
#endif
#define DelayUs(x) { \
delayus_variable=(unsigned char)(x/DelayDivisor); \
WaitFor1Us; } \
asm("decfsz _delayus_variable,f"); \
Jumpback;
/*
timeouts:
C code for testing with ints:
unsigned int timeout;
timeout=4000;
PORT_DIRECTION=OUTPUT;
while(1)
{
PORT=1;
timeout=8000;
while(timeout-- >= 1); //60ms @ 8Mhz, opt on, 72ms @ 8Mhz, opt off
PORT=0;
}
Time taken: optimisations on: 16cyc/number loop, 8us @ 8Mhz
optimisations off: 18cyc/number loop, 9us @ 8Mhz
with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz
C code for testing with chars:
similar to above
Time taken: optimisations on: 9cyc/number loop, 4.5us @ 8Mhz
with extra check ie: && (RB7==1), +3cyc/number loop, +1.5us @ 8Mhz
Formula: rough timeout value = (<us desired>/<cycles per loop>) * (PIC_CLK/4.0)
To use: //for max timeout of 1147us @ 8Mhz
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (unsigned char)((x/LOOP_CYCLES_CHAR)*(PIC_CLK/4.0))
unsigned char timeout;
timeout=timeout_char_us(1147); //max timeout allowed @ 8Mhz, 573us @ 16Mhz
while((timeout-- >= 1) && (<extra condition>)); //wait
To use: //for max 491512us, half sec timeout @ 8Mhz
#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (unsigned int)((x+/LOOP_CYCLES_INT)*(PIC_CLK/4.0))
unsigned int timeout;
timeout=timeout_int_us(491512); //max timeout allowed @ 8Mhz
while((timeout-- >= 1) && (<extra condition>)); //wait
*/
#define LOOP_CYCLES_CHAR 9 //how many cycles per loop, optimizations on
#define timeout_char_us(x) (long)(((x)/LOOP_CYCLES_CHAR)*(PIC_CLK/1000000/4))
#define LOOP_CYCLES_INT 16 //how many cycles per loop, optimizations on
#define timeout_int_us(x) (long)(((x)/LOOP_CYCLES_INT)*(PIC_CLK/1000000/4))
//if lo byte is zero, faster initialization by 1 instrucion
#define timeout_int_lobyte_zero_us(x) (long)(((x)/LOOP_CYCLES_INT)*(PIC_CLK/4.0)&0xFF00)
//function prototypes
void DelayBigUs(unsigned int cnt);
void DelayMs(unsigned char cnt);
void DelayMs_interrupt(unsigned char cnt);
void DelayBigMs(unsigned int cnt);
void DelayS(unsigned char cnt);
#endif
settaggio.h
- Codice: Seleziona tutto
#define SER1 RB0 //segnale servo1
#define PL1 RB1 // puls1
#define PL2 RB2 // puls2
#define SER2 RB3 //segnale servo2
#define LED RA0 //LED rete
#define N3 RB5
#define N4 RB6
#define N5 RB7
#define N6 RA1
#define N7 RA2
#define PL3 RA4 //puls3
#define N2 RB4
#define N8 RA3
#define XTAL_FREQ 4MHZ
#define _XTAL_FREQ 4MHZ
unsigned char i;
int a=1;
unsigned char m;
void settaggio(void)
{
OPTION=0b10000000;
TRISA=0b00010000;
TRISB=0b00000110;
N2=0;
LED=0;
SER1=0;
SER2=0;
N3=0;
N4=0;
N5=0;
N6=0;
N7=0;
N8=0;
}
PIC.h
- Codice: Seleziona tutto
#ifndef _PIC_H
#define _PIC_H
#if defined(_12C508) || defined(_12C509)
#include <pic1250x.h>
#endif
#if defined(_16C52) || defined(_16C54) || defined(_16C54A) ||\
defined(_16CR54A) || defined(_16C55) || defined(_16C56) ||\
defined(_16C57) || defined(_16CR57B) || defined(_16C58A) ||\
defined(_16CR58A) || defined(_16C58)
#include <pic165x.h>
#endif
#if defined(_12C671) || defined(_12C672)
#include <pic1267x.h>
#endif
#ifdef _14000
#include <pic14000.h>
#endif
#if defined(_16C554) || defined(_16C556) || defined(_16C558)
#include <pic1655x.h>
#endif
#ifdef _16C61
#include <pic1661.h>
#endif
#if defined(_16C62) || defined(_16C62A) || defined(_16CR62)
#include <pic1662.h>
#endif
#if defined(_16C620) || defined(_16C621) || defined(_16C622)
#include <pic1662x.h>
#endif
#if defined(_16C63) || defined(_16CR63)
#include <pic1663.h>
#endif
#if defined(_16C64) || defined(_16C64A) || defined(_16CR64)
#include <pic1664.h>
#endif
#if defined(_16C641) || defined(_16C642) ||\
defined(_16C661) || defined(_16C662)
#include <pic166xx.h>
#endif
#if defined(_16C65) || defined(_16C65A) || defined(_16CR65)
#include <pic1665.h>
#endif
#if defined(_16C66) || defined(_16C67)
#include <pic166x.h>
#endif
#if defined(_16C71) || defined(_16C710) || defined(_16C711)
#include <pic1671x.h>
#endif
#ifdef _16C715
#include <pic16715.h>
#endif
#ifdef _16C72
#include <pic1672.h>
#endif
#if defined(_16C73) || defined(_16C73A)
#include <pic1673.h>
#endif
#if defined(_16C74) || defined(_16C74A)
#include <pic1674.h>
#endif
#ifdef _16C76
#include <pic1676.h>
#endif
#ifdef _16C77
#include <pic1677.h>
#endif
#if defined(_16F83) || defined(_16CR83)
#include <pic1683.h>
#endif
#if defined(_16C84) || defined(_16F84) || defined(_16CR84)
#include <pic1684.h>
#endif
#if defined(_16C923) || defined(_16C924)
#include <pic169xx.h>
#endif
#ifdef _16C99
#include <pic1699.h>
#endif
#ifdef _17C42
#include <pic1742.h>
#endif
#define CLRWDT() asm(" clrwdt")
#define ___mkstr(x) #x
#define __CONFIG(x) asm("\tpsect config,class=CODE,delta=2");\
asm("\tglobal\tconfig_word"); \
asm("config_word"); \
asm("\tdw "___mkstr(x))
#endif /* _PIC_H */
Always.h
- Codice: Seleziona tutto
/*
Common header file
Designed by Shane Tolmie of www.microchipC.com corporation. Freely distributable.
Questions and comments to webmaster@microchipC.com
Lots of Hi-Tech C FAQ and sample source code at www.microchipC.com.
For Microchip PIC16Fx.
Compiled with Hitech-C v7.85
Usage: #include in all ".c" files in project along with "PIC.h"
*/
//warning about #if statements: if any of the arguments are spelled wrong or
//unrecognised by the compiler, it will not generate a warning, but not include code
#ifndef ALWAYS_H
#define ALWAYS_H
/*
Turning bits on/off according to mask
use ~0 instead of 0xFF, etc, because this ensures machine independence
if int changes from 16-bit to 32-bit
Example C:
x=0b001;
bits_on(x,0b100) //now x=0b101
*/
#define bits_on(var,mask) var |= mask
#define bits_off(var,mask) var &= ~0 ^ mask
//defines
#define INPUT 1 //port directions, ie: TRISA0=INPUT;
#define OUTPUT 0
#define TRUE 1
#define FALSE 0
#define HIGH 1
#define LOW 0
#define hi 1
#define lo 0
#define b asm("nop") //convenient point for breakpoint (debugging)
#define l while(1) //loop for ever (debugging)
//see AM576. If interrupt occurs just when gie gets set to zero, it won't be cleared
#define gie_on GIE=1
#define gie_off while(GIE==1) GIE=0
/*
Reading an 8-bit byte in a 16-bit int
With Hi-Tech C, this method is better than using pointers, as using pointers in
different banks needs different #defines
It is just as efficient - the optimizer picks out the correct
byte. Of course, >>7 requires 7 shifts.
This method cannot be used to alter a hi/lo byte, this needs pointers (as below)
Example C:
unsigned int x;
unsigned char y;
x=0x1234;
y=hibyte(x); //now y=0x12 - works for variables in any bank 0 to 3
y=lobyte(x); //now y=0x34
lobyte(x)=0xaa; //will not work :( - use pointers
*/
#define hibyte(x) (unsigned char)(x>>8)
#define lobyte(x) (unsigned char)(x & 0xFF)
/*
given variable of any type (char, uchar, int, uint, long) it modifies
the unsigned char residing at that memory location
for ints, byte1 is msb, byte0 is lsb (least significant byte)
for longs byte3 is msb, byte0 is lsb
ie: sample C code
unsigned int myint=0x4321;
long mylong=0x87654321;
//for myint byte1(myint)=0x43; (msb) and byte0(myint)=0x21; (lsb)
//for mylong byte3(mylong)=0x87; (msb), byte2(mylong)=0x65;
byte2(mylong)=0x43; and byte0(mylong)=0x21; (lsb)
note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/
#define byte0(x) (unsigned char)(*(((unsigned char *)&x)+0))
#define byte1(x) (unsigned char)(*(((unsigned char *)&x)+1))
#define byte2(x) (unsigned char)(*(((unsigned char *)&x)+2))
#define byte3(x) (unsigned char)(*(((unsigned char *)&x)+3))
#define lobyte_atbank0 byte0 //another way of saying it
#define hibyte_atbank0 byte1
#define byte0_atbank1(x) (unsigned char)(*(((bank1 unsigned char *)&x)+0))
#define byte1_atbank1(x) (unsigned char)(*(((bank1 unsigned char *)&x)+1))
#define byte2_atbank1(x) (unsigned char)(*(((bank1 unsigned char *)&x)+2))
#define byte3_atbank1(x) (unsigned char)(*(((bank1 unsigned char *)&x)+3))
#define lobyte_atbank1 byte0_atbank1 //another way of saying it
#define hibyte_atbank1 byte1_atbank1
#define byte0_atbank2(x) (unsigned char)(*(((bank2 unsigned char *)&x)+0))
#define byte1_atbank2(x) (unsigned char)(*(((bank2 unsigned char *)&x)+1))
#define byte2_atbank2(x) (unsigned char)(*(((bank2 unsigned char *)&x)+2))
#define byte3_atbank2(x) (unsigned char)(*(((bank2 unsigned char *)&x)+3))
#define byte0_atbank3(x) (unsigned char)(*(((bank3 unsigned char *)&x)+0))
#define byte1_atbank3(x) (unsigned char)(*(((bank3 unsigned char *)&x)+1))
#define byte2_atbank3(x) (unsigned char)(*(((bank3 unsigned char *)&x)+2))
#define byte3_atbank3(x) (unsigned char)(*(((bank3 unsigned char *)&x)+3))
/*
given variable of any type (char, uchar, int, uint, long) it modifies
the int residing at that memory location
ie: sample C code
unsigned char array[4];
unsigned int test;
uint_atbyteaddr(&array[0])=0x4321; //now array[0->3]={0x21,0x43,0,0};
uint_atbyteaddr(&array[0+2])=0x8765; //now array[0->3]={0x21,0x43,0x65,0x87};
test=uint_atbyteaddr(&array[0+2]) //now test=0x8765
note: do NOT use &(array[0]+1) to reference the int stored at array[1] as it will
reference the int after array[0] in pointer arithmetic. This will
result with the int at array[2].
Instead use &array[0+1] to reference the int at uchar array[1]
note: to avoid fixup overflow errors add bankX if the target variable
resides in banks 1, 2 or 3
*/
#define uint_atbyteaddr(x) (unsigned int)(*(((unsigned int *)x)))
#define uint_atbank1byteaddr(x) (unsigned int)(*(((bank1 unsigned int *)x)))
#define uint_atbank2byteaddr(x) (unsigned int)(*(((bank2 unsigned int *)x)))
#define uint_atbank3byteaddr(x) (unsigned int)(*(((bank3 unsigned int *)x)))
#define THE_BEER_IS_PLENTIFUL_AND_THE_PARTY_SWINGING TRUE
/*
NOTE: it is not recommended that unions are used to reference hi/lo bytes or
bits of a variable. Use >>8 or &FF or pointers instead, as above. It makes
passing variables to a function difficult, as the function must be defined to
accept variables of the same union. Then, the function will no longer accept
normally defined variables.
these two structures allow access to 2 byte word, high and low bytes of variable
declaration: union wordtype x;
usage: x.word=0xABCD; x.byte.high=0xAB; x.byte.low=0xCD;
x.part.bit15=1; (msb), x.part.bit0=1; (lsb)
declaration: union chartype x;
usage: x.byte=0xAB;
x.part.bit7=1; (msb), x.part.bit0=1; (lsb)
*/
struct sixteen_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
unsigned char bit8 :1;
unsigned char bit9 :1;
unsigned char bit10 :1;
unsigned char bit11 :1;
unsigned char bit12 :1;
unsigned char bit13 :1;
unsigned char bit14 :1;
unsigned char bit15 :1;
};
struct eight_bits
{
unsigned char bit0 :1;
unsigned char bit1 :1;
unsigned char bit2 :1;
unsigned char bit3 :1;
unsigned char bit4 :1;
unsigned char bit5 :1;
unsigned char bit6 :1;
unsigned char bit7 :1;
};
struct two_bytes
{
unsigned char low;
unsigned char high;
};
union wordtype
{
unsigned int word;
struct two_bytes byte;
struct sixteen_bits part;
};
union chartype
{
unsigned char byte;
struct eight_bits part;
};
#endif
Grazie
Buona serata Marco