Inversor de 3 fases pic16f872

Ver o tópico anterior Ver o tópico seguinte Ir em baixo

Inversor de 3 fases pic16f872

Mensagem por whiplash em Sex 19 Dez - 14:00:34

Boa Tarde pessoal.
Esse é meu primeiro post aqui no forum, e vou colocar o meu projetinho:
É basicamente um controlador de uma ponte h com 3 fases, um controlador de LCD 16x2 e um controlador de ponte h (simples)com 2 fases.
Eu utilizo ele como um 'gadget' apenas, a ponte h de 3 fases é para um motor de hd, o lcd eu estou implementando data, hora, temperatura e umidade, e a ponte h normal é para o atuador (agulha) do hd.
meu projeto tá no começo ainda: ele roda o motor de 3 fases, ele escreve no lcd 16x2.
Falta bastante coisa, mas vai ser um projeto bem legal.
O meu problema inicial, é que o controle das 3 fases não é por pwm (não sei programar) e como escrever no lcd leva alguns us, sempre um passo está dando uma travadinha.... mas acho que com pwm isso se resolva....
schm:

Codigo .c:
Código:
#include <16F872.h>
#FUSES NOWDT
#FUSES NOPROTECT
#FUSES NOPUT
#FUSES NOBROWNOUT
#FUSES HS
#use delay(crystal=20000000)
#define T1 PIN_C1
#define T2 PIN_C2
#define T3 PIN_C3
#define T4 PIN_C4
#define T5 PIN_C5
#define T6 PIN_C6
#define led PIN_A2
#include <lcd.c>
#define LCD_DATA_PORT getenv("SFR:PORTB")
int x=0;
int y=0;
void passo1(){
output_low(T1);
output_high(T2);
output_low(T3);
output_low(T4);
output_low(T5);
output_high(T6);
}
void passo2(){
output_high(T1);
output_low(T2);
output_low(T3);
output_low(T4);
output_low(T5);
output_high(T6);
}
void passo3(){
output_high(T1);
output_low(T2);
output_low(T3);
output_high(T4);
output_low(T5);
output_low(T6);
}
void passo4(){
output_low(T1);
output_low(T2);
output_low(T3);
output_high(T4);
output_high(T5);
output_low(T6);
}
void passo5(){
output_low(T1);
output_high(T2);
output_low(T3);
output_low(T4);
output_high(T5);
output_low(T6);
}
void passo6(){
output_low(T1);
output_high(T2);
output_high(T3);
output_low(T4);
output_low(T5);
output_low(T6);
}
void acelera() {
for (x=1 ; x<100;x++){
passo1();
delay_ms(150-x);
passo2();
delay_ms(150-x);
passo3();
delay_ms(150-x);
passo4();
delay_ms(150-x);
passo5();
delay_ms(150-x);
passo6();
delay_ms(146-x);
printf(lcd_putc,"\facelerando-> %u", x);
    }}
void main () {
lcd_init();
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0_AN1_AN3);
set_adc_channel(0);
//acelera();
y=0;
while (1) {
x=10;
passo1();
delay_ms(x);
passo2();
delay_ms(x);
passo3();
delay_ms(x);
passo4();
delay_ms(x);
passo5();
delay_ms(x);
passo6();
delay_ms(x-3);
y++;
printf(lcd_putc,"\fcontando -> %u", y);
}}
biblioteca do pic:
Código:
//////// Standard Header file for the PIC16F73 device ////////////////
#device PIC16F73

#nolist
//////// Program memory: 4096x14  Data RAM: 192  Stack: 8
//////// I/O: 22  Analog Pins: 5
//////// C Scratch area: 20  ID Location: 2000
//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,PUT,NOPUT,PROTECT,NOPROTECT,NOBROWNOUT
//////// Fuses: BROWNOUT
////////
#if !defined(__PCM__)
#define _bif
#define int8 char
#define int16 long
#define int32 long long
#define float32 float
#define int1 char

#endif
////////////////////////////////////////////////////////////////// I/O
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),
//                        PORT_x_PULLUPS(), INPUT(),
//                        OUTPUT_LOW(), OUTPUT_HIGH(),
//                        OUTPUT_FLOAT(), OUTPUT_BIT()
// Discrete I/O Prototypes:
_bif void set_tris_a(int8 value); 
_bif void set_tris_b(int8 value); 
_bif void set_tris_c(int8 value); 
_bif int8 get_tris_a(void); 
_bif int8 get_tris_b(void); 
_bif int8 get_tris_c(void); 
_bif void output_a(int8 value); 
_bif void output_b(int8 value); 
_bif void output_c(int8 value); 
_bif int8 input_a(void); 
_bif int8 input_b(void); 
_bif int8 input_c(void); 
_bif int1 input_change_a(void); 
_bif int1 input_change_b(void); 
_bif int1 input_change_c(void); 
_bif int1 input(int16 pin);
_bif int1 input_state(int16 pin);
_bif void output_low(int16 pin);
_bif void output_high(int16 pin);
_bif void output_toggle(int16 pin);
_bif void output_bit(int16 pin, int1 level);
_bif void output_float(int16 pin);
_bif void output_drive(int16 pin);

// 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
#define FALSE 0
#define TRUE 1

#define BYTE int8
#define BOOLEAN int1

#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()
// Prototypes:
_bif int8 restart_cause(void);
_bif void reset_cpu(void);
_bif void sleep(void);
// Constants returned from RESTART_CAUSE() are:
#define WDT_FROM_SLEEP    3 
#define WDT_TIMEOUT      11 
#define MCLR_FROM_SLEEP  19 
#define MCLR_FROM_RUN    27 
#define NORMAL_POWER_UP  25 
#define BROWNOUT_RESTART 26 

////////////////////////////////////////////////////////////////// Timer 0
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER_0(),
//                              SET_TIMER0() or SET_RTCC(),
//                              GET_TIMER0() or GET_RTCC()
// Timer 0 Prototypes:
_bif void setup_timer_0(int8 mode);
_bif void set_timer0(int8 value);     
_bif int8 get_timer0(void);           
_bif void setup_counters(int8 mode, int8 prescaler);
_bif void set_rtcc(int8 value);       
_bif int8 get_rtcc(void);             
// Constants used for SETUP_TIMER_0() are:
#define T0_INTERNAL  0 
#define T0_EXT_L_TO_H 32
#define T0_EXT_H_TO_L 48

#define T0_DIV_1      8
#define T0_DIV_2      0
#define T0_DIV_4      1
#define T0_DIV_8      2
#define T0_DIV_16    3
#define T0_DIV_32    4
#define T0_DIV_64    5
#define T0_DIV_128    6
#define T0_DIV_256    7


#define T0_8_BIT      0   

#define RTCC_INTERNAL  0      // The following are provided for compatibility
#define RTCC_EXT_L_TO_H 32    // with older compiler versions
#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()
// WDT base is 18ms
// WDT Prototypes:
_bif void setup_wdt(int16 mode);
_bif void restart_wdt(void);
// Constants used for SETUP_WDT() are:

#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
// Timer 1 Prototypes:
_bif void setup_timer_1(int16 mode);
_bif int16 get_timer1(void);
_bif void set_timer1(int16 value);
// 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
// Timer 2 Prototypes:
_bif void setup_timer_2(int8 mode, int8 period, int8 postscale);
_bif int8 get_timer2(void);
_bif void set_timer2(int8 value);
// 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
// CCP1 Prototypes:
_bif void setup_ccp1(int32 mode);
_bif void setup_ccp1(int32 mode, int8 pwm);
_bif void set_pwm1_duty(int16 value);
// 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
#word  CCP_1      =                  getenv("SFR:CCPR1L")
#byte  CCP_1_LOW  =                  getenv("SFR:CCPR1L")
#byte  CCP_1_HIGH  =                  getenv("SFR:CCPR1H")

// CCP2 Prototypes:
_bif void setup_ccp2(int32 mode);
_bif void setup_ccp2(int32 mode, int8 pwm);
_bif void set_pwm2_duty(int16 value);
#word  CCP_2      =                  getenv("SFR:CCPR2L")
#byte  CCP_2_LOW  =                  getenv("SFR:CCPR2L")
#byte  CCP_2_HIGH  =                  getenv("SFR:CCPR2H")
////////////////////////////////////////////////////////////////// SPI
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN
// SPI Prototypes:
_bif void setup_spi(int32 mode);
_bif void spi_write(int8 value);
_bif int8 spi_read(void);
_bif int8 spi_read(int8 value);
_bif int1 spi_data_in(void);
// Constants used in SETUP_SPI() are:
#define SPI_DISABLED            0x00
#define SPI_MASTER              0x20
#define SPI_SLAVE                0x24
#define SPI_SCK_IDLE_HIGH        0x10
#define SPI_SCK_IDLE_LOW        0x00
#define SPI_CLK_DIV_4            0x00
#define SPI_CLK_DIV_16          0x01
#define SPI_CLK_DIV_64          0x02
#define SPI_CLK_T2              0x03
#define SPI_SS_DISABLED          0x01

#define SPI_XMIT_L_TO_H          0x4000
#define SPI_XMIT_H_TO_L          0x0000

#define SPI_SAMPLE_AT_MIDDLE    0x0000
#define SPI_SAMPLE_AT_END        0x8000

//The following are provided for compatibility
#define SPI_L_TO_H      SPI_SCK_IDLE_LOW
#define SPI_H_TO_L      SPI_SCK_IDLE_HIGH

////////////////////////////////////////////////////////////////// UART
// UART Prototypes:
_bif void setup_uart(int32 baud);
_bif void setup_uart(int32 baud, int8 stream);
_bif void setup_uart(int32 baud, int8 stream, int32 clock);
_bif void set_uart_speed(int32 baud);
_bif void set_uart_speed(int32 baud, int8 stream);
_bif void set_uart_speed(int32 baud, int8 stream, int32 clock);
// Constants used in setup_uart() are:
// FALSE - Turn UART off
// TRUE  - Turn UART on
#define UART_ADDRESS          2
#define UART_DATA              4


////////////////////////////////////////////////////////////////// ADC
// ADC Functions: SETUP_ADC(), SETUP_ADC_PORTS() (aka SETUP_PORT_A),
//                SET_ADC_CHANNEL(), READ_ADC()
// ADC Prototypes:
_bif void setup_adc(int16 mode);
_bif int8 read_adc(void);
_bif int8 read_adc(int8 mode);
_bif int16 read_adc(void);
_bif int16 read_adc(int8 mode);
_bif int1 adc_done(void);
// 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

//ADC Prototypes:
_bif void setup_adc_ports(int8 setting);
_bif void set_adc_channel(int8 channel);
// 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 ANALOG_RA3_REF                        1  //!old only provided for compatibility
#define RA0_RA1_RA3_ANALOG                    4  //!old only provided for compatibility
#define RA0_RA1_ANALOG_RA3_REF                5  //!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

////////////////////////////////////////////////////////////////// BIF
// Built In Functions Prototypes
//
// Math Prototypes:
_bif signed int8 abs(signed int8 x);
_bif signed int16 abs(signed int16 x);
_bif signed int32 abs(signed int32 x);
_bif float32 abs(float32 x);
_bif unsigned int16 _mul(unsigned int8, unsigned int8);
_bif signed int16 _mul(signed int8, signed int8);
_bif unsigned int32 _mul(unsigned int16, unsigned int16);
_bif signed int32 _mul(signed int16, signed int16);

// Memory Manipulation Prototypes:
_bif int8 read_bank(int8 bank, int8 offset);
_bif void write_bank(int8 bank, int8 offset, int8 value);
_bif void strcpy(char* dest, char* src);
_bif void strcopy(char* dest, char* src);
_bif void memset(unsigned int8* destination, unsigned int8 value, unsigned int16 num);
_bif void memcpy(unsigned int8* destination, unsigned int8* source, unsigned int16 num);

// String Prototypes:
_bif char toupper(char cvalue);
_bif char tolower(char cvalue);
_bif void sprintf(char* string, char* cstring, ...);

// Data Manipulators Prototypes:
_bif int1 shift_left(unsigned int8* address, unsigned int8 bytes, int1 value);
_bif int1 shift_right(unsigned int8* address, unsigned int8 bytes, int1 value);
_bif void rotate_left(unsigned int8* address, unsigned int8 bytes);
_bif void rotate_right(unsigned int8* address, unsigned int8 bytes);
_bif void swap(unsigned int8 value);
_bif unsigned int8 make8(unsigned int16 var, unsigned int8 offset);
_bif unsigned int8 make8(unsigned int32 var, unsigned int8 offset);
_bif unsigned int16 make16(unsigned int8 varhigh, unsigned int8 varlow);
_bif unsigned int32 make32(unsigned int16 var1);
_bif unsigned int32 make32(unsigned int16 var1, unsigned int16 var2);
_bif unsigned int32 make32(unsigned int16 var1, unsigned int8 var2);
_bif unsigned int32 make32(unsigned int16 var1, unsigned int8 var2, unsigned int8 var3);
_bif unsigned int32 make32(unsigned int8 var1);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int8 var2);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int8 var2, unsigned int8 var3);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int8 var2, unsigned int8 var3, unsigned int8 var4);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int16 var2);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int16 var2, unsigned int8 var3);
_bif unsigned int32 make32(unsigned int8 var1, unsigned int8 var2, unsigned int16 var3);
_bif void bit_set(unsigned int8 var, unsigned int8 bit);
_bif void bit_set(unsigned int16 var, unsigned int8 bit);
_bif void bit_set(unsigned int32 var, unsigned int8 bit);
_bif void bit_clear(unsigned int8 var, unsigned int8 bit);
_bif void bit_clear(unsigned int16 var, unsigned int8 bit);
_bif void bit_clear(unsigned int32 var, unsigned int8 bit);
_bif int1 bit_test(unsigned int8 var, unsigned int8 bit);
_bif int1 bit_test(unsigned int16 var, unsigned int8 bit);
_bif int1 bit_test(unsigned int32 var, unsigned int8 bit);

// #use delay() Prototypes:
_bif void delay_cycles(unsigned int8 count);
_bif void delay_ms(unsigned int16 time);
_bif void delay_us(unsigned int16 time);

// #use rs232() Prototypes:
_bif void putchar(char cdata);
_bif void putchar(char cdata, unsigned int8 stream);
_bif void puts(char* string);
_bif void puts(char* string, unsigned int8 stream);
_bif char getch(void);
_bif char getch(unsigned int8 stream);
_bif void gets(char* string);
_bif void gets(char* string, unsigned int8 stream);
_bif int1 kbhit(void);
_bif int1 kbhit(unsigned int8 stream);
_bif void printf(char* string, ...);
_bif void fprintf(unsigned int8 stream, char* string, ...);
_bif void putc_send(void);
_bif void fputc_send(unsigned int8 stream);
_bif int1 rcv_buffer_full(void);
_bif int1 rcv_buffer_full(unsigned int8 stream);
_bif unsigned int16 rcv_buffer_bytes(void);
_bif unsigned int16 rcv_buffer_bytes(unsigned int8 stream);
_bif int1 tx_buffer_full(void);
_bif int1 tx_buffer_full(unsigned int8 stream);
_bif unsigned int16 tx_buffer_bytes(void);
_bif unsigned int16 tx_buffer_bytes(unsigned int8 stream);

// #use i2c() Prototypes:
_bif unsigned int8 i2c_read(void);
_bif unsigned int8 i2c_read(unsigned int8 stream);
_bif unsigned int8 i2c_read(unsigned int8 stream, int1 ack);
_bif int1 i2c_write(unsigned int8 data);
_bif int1 i2c_write(unsigned int8 stream, unsigned int8 data);
_bif void i2c_start(void);
_bif void i2c_start(unsigned int8 stream);
_bif void i2c_start(unsigned int8 stream, unsigned int8 restart);
_bif void i2c_stop(void);
_bif void i2c_stop(unsigned int8 stream);
_bif int8 i2c_isr_state(void);
_bif void i2c_slaveaddr(unsigned int8 addr);
_bif void i2c_slaveaddr(unsigned int8 stream, unsigned int8 addr);
_bif int1 i2c_poll(void);
_bif int1 i2c_poll(unsigned int8 stream);
_bif void i2c_init(unsigned int32 baud);
_bif void i2c_init(unsigned int8 stream, unsigned int32 baud);

// #use spi() Prototypes:
_bif unsigned int8 spi_xfer(void);
_bif unsigned int16 spi_xfer(void);
_bif unsigned int32 spi_xfer(void);
_bif unsigned int8 spi_xfer(unsigned int8 data);
_bif unsigned int16 spi_xfer(unsigned int16 data);
_bif unsigned int32 spi_xfer(unsigned int32 data);
_bif unsigned int8 spi_xfer(unsigned int8 stream, unsigned int8 data);
_bif unsigned int16 spi_xfer(unsigned int8 stream, unsigned int16 data);
_bif unsigned int32 spi_xfer(unsigned int8 stream, unsigned int32 data);
_bif unsigned int8 spi_xfer(unsigned int8 stream, unsigned int8 data, unsigned int8 bits);
_bif unsigned int16 spi_xfer(unsigned int8 stream, unsigned int16 data, unsigned int8 bits);
_bif unsigned int32 spi_xfer(unsigned int8 stream, unsigned int32 data, unsigned int8 bits);
_bif void spi_init(unsigned int32 baud);
_bif void spi_init(unsigned int8 stream, unsigned int32 baud);
_bif void spi_speed(unsigned int32 baud);
_bif void spi_speed(unsigned int8 stream, unsigned int32 baud);
_bif void spi_speed(unsigned int8 stream, unsigned int32 baud, unsigned int32 clock);
_bif void spi_prewrite(unsigned int8 data);
_bif void spi_prewrite(unsigned int16 data);
_bif void spi_prewrite(unsigned int32 data);
_bif void spi_prewrite(unsigned int8, unsigned int8 data);
_bif void spi_prewrite(unsigned int8, unsigned int16 data);
_bif void spi_prewrite(unsigned int8, unsigned int32 data);
_bif unsigned int8 spi_xfer_in(void);
_bif unsigned int16 spi_xfer_in(void);
_bif unsigned int32 spi_xfer_in(void);
_bif unsigned int8 spi_xfer_in(unsigned int8 bits);
_bif unsigned int16 spi_xfer_in(unsigned int8 bits);
_bif unsigned int32 spi_xfer_in(unsigned int8 bits);
_bif unsigned int8 spi_xfer_in(unsigned int8 stream, unsigned int8 bits);
_bif unsigned int16 spi_xfer_in(unsigned int8 stream, unsigned int8 bits);
_bif unsigned int32 spi_xfer_in(unsigned int8 stream, unsigned int8 bits);

// #use rtos() Prototypes:
_bif void rtos_run(void);
_bif void rtos_yield(void);
_bif void rtos_enable(unsigned int8 task);
_bif void rtos_disable(unsigned int8 task);
_bif void rtos_terminate(void);
_bif void rtos_await(int1 flag);
_bif void rtos_wait(unsigned int8 sem);
_bif void rtos_signal(unsigned int8 sem);
_bif void rtos_msg_send(unsigned int8 task, unsigned int8 msg);
_bif unsigned int8 rtos_msg_read(void);
_bif unsigned int8 rtos_msg_poll(void);
_bif int1 rtos_overrun(unsigned int8 task);
_bif void rtos_stats(unsigned int8 task, unsigned int8* stat);

// #use timer() Prototypes:
_bif unsigned int8 get_ticks(void);
_bif unsigned int16 get_ticks(void);
_bif unsigned int32 get_ticks(void);
_bif unsigned int8 get_ticks(unsigned int8 stream);
_bif unsigned int16 get_ticks(unsigned int8 stream);
_bif unsigned int32 get_ticks(unsigned int8 stream);
_bif void set_ticks(unsigned int8 value);
_bif void set_ticks(unsigned int16 value);
_bif void set_ticks(unsigned int32 value);
_bif void set_ticks(unsigned int8 stream, unsigned int8 value);
_bif void set_ticks(unsigned int8 stream, unsigned int16 value);
_bif void set_ticks(unsigned int8 stream, unsigned int32 value);

// #use pwm() Prototypes:
_bif void pwm_on(void);
_bif void pwm_on(unsigned int8 stream);
_bif void pwm_off(void);
_bif void pwm_off(unsigned int8 stream);
_bif void pwm_set_duty(unsigned int16 duty);
_bif void pwm_set_duty(unsigned int8 stream, unsigned int16 duty);
_bif void pwm_set_duty_percent(unsigned int16 percent);
_bif void pwm_set_duty_percent(unsigned int8 stream, unsigned int16 percent);
_bif void pwm_set_frequency(unsigned int32 frequency);
_bif void pwm_set_frequency(unsigned int8 stream, unsigned int32 frequency);

// #use capture() Prototypes:
_bif unsigned int16 get_capture_time(void);
_bif unsigned int16 get_capture_time(unsigned int8 stream);
_bif int1 get_capture_event(void);
_bif int1 get_capture_event(unsigned int8 stream);

// Enviroment Prototypes:
//_bif unsigned int8 getenv(char* cstring);

// Address Prototypes:
#ifndef __ADDRESS__
 #if defined(__PCM__) || define(__PCB__)
  typedef unsigned int16 __ADDRESS__;
 #else
  typedef unsigned int32 __ADDRESS__;
 #endif
#endif
_bif void goto_address(__ADDRESS__ address);
_bif __ADDRESS__ label_address(__ADDRESS__ label);

// Program Memory Prototypes:
_bif void read_program_memory(__ADDRESS__ address, unsigned int8* dataptr, unsigned int16 count);
_bif unsigned int16 read_program_eeprom(__ADDRESS__ address);
////////////////////////////////////////////////////////////////// INT
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
//                      CLEAR_INTERRUPT(), INTERRUPT_ACTIVE(),
//                      EXT_INT_EDGE()
// INT Prototoypes:
_bif void enable_interrupts(int32 interrupt);
_bif void disable_interrupts(int32 interrupt);
_bif void clear_interrupt(int32 interrupt);
_bif int1 interrupt_active(int32 interrupt);
_bif void ext_int_edge(int8 source, int8 edge);
_bif void jump_to_irs(int16 address);
// 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 PERIPH                    0x0B40
#define INT_RTCC                  0x000B20
#define INT_RB                    0x00FF0B08
#define INT_EXT_L2H              0x50000B10
#define INT_EXT_H2L              0x60000B10
#define INT_EXT                  0x000B10
#define INT_AD                    0x008C40
#define INT_TBE                  0x008C10
#define INT_RDA                  0x008C20
#define INT_TIMER1                0x008C01
#define INT_TIMER2                0x008C02
#define INT_CCP1                  0x008C04
#define INT_CCP2                  0x008D01
#define INT_SSP                  0x008C08
#define INT_TIMER0                0x000B20

#list
biblioteca do lcd:
Código:
///////////////////////////////////////////////////////////////////////////////
////                            LCD.C                                    ////
////                Driver for common LCD modules                        ////
////                                                                      ////
////  lcd_init()  Must be called before any other function.              ////
////                                                                      ////
////  lcd_putc(c)  Will display c on the next position of the LCD.        ////
////                \a  Set cursor position to upper left                ////
////                \f  Clear display, set cursor to upper left          ////
////                \n  Go to start of second line                        ////
////                \b  Move back one position                            ////
////              If LCD_EXTENDED_NEWLINE is defined, the \n character    ////
////              will erase all remanining characters on the current      ////
////              line, and move the cursor to the beginning of the next  ////
////              line.                                                    ////
////              If LCD_EXTENDED_NEWLINE is defined, the \r character    ////
////              will move the cursor to the start of the current        ////
////              line.                                                    ////
////                                                                      ////
////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)        ////
////                                                                      ////
////  lcd_getc(x,y)  Returns character at position x,y on LCD            ////
////                                                                      ////
////  lcd_cursor_on(int1 on)  Turn the cursor on (on=TRUE) or off        ////
////              (on=FALSE).                                              ////
////                                                                      ////
////  lcd_set_cgram_char(w, *p)  Write a custom character to the CGRAM.  ////
////                                                                      ////
////                                                                      ////
////  CONFIGURATION                                                        ////
////  The LCD can be configured in one of two ways: a.) port access or    ////
////  b.) pin access.  Port access requires the entire 7 bit interface    ////
////  connected to one GPIO port, and the data bits (D4:D7 of the LCD)    ////
////  connected to sequential pins on the GPIO.  Pin access                ////
////  has no requirements, all 7 bits of the control interface can        ////
////  can be connected to any GPIO using several ports.                    ////
////                                                                      ////
////  To use port access, #define LCD_DATA_PORT to the SFR location of    ////
////  of the GPIO port that holds the interface, -AND- edit LCD_PIN_MAP    ////
////  of this file to configure the pin order.  If you are using a        ////
////  baseline PIC (PCB), then LCD_OUTPUT_MAP and LCD_INPUT_MAP also must  ////
////  be defined.                                                          ////
////                                                                      ////
////  Example of port access:                                              ////
////    #define LCD_DATA_PORT getenv("SFR:PORTD")                        ////
////                                                                      ////
////  To use pin access, the following pins must be defined:              ////
////    LCD_ENABLE_PIN                                                    ////
////    LCD_RS_PIN                                                        ////
////    LCD_RW_PIN                                                        ////
////    LCD_DATA4                                                        ////
////    LCD_DATA5                                                        ////
////    LCD_DATA6                                                        ////
////    LCD_DATA7                                                        ////
////                                                                      ////
////  Example of pin access:                                              ////
////    #define LCD_ENABLE_PIN  PIN_E0                                    ////
////    #define LCD_RS_PIN      PIN_E1                                    ////
////    #define LCD_RW_PIN      PIN_E2                                    ////
////    #define LCD_DATA4      PIN_D4                                    ////
////    #define LCD_DATA5      PIN_D5                                    ////
////    #define LCD_DATA6      PIN_D6                                    ////
////    #define LCD_DATA7      PIN_D7                                    ////
////                                                                      ////
///////////////////////////////////////////////////////////////////////////////
////        (C) Copyright 1996,2010 Custom Computer Services          ////
//// This source code may only be used by licensed users of the CCS C  ////
//// compiler.  This source code may only be distributed to other      ////
//// licensed users of the CCS C compiler.  No other use, reproduction ////
//// or distribution is permitted without written permission.          ////
//// Derivative programs created using this software in object code    ////
//// form are not restricted in any way.                              ////
///////////////////////////////////////////////////////////////////////////

#ifndef __LCD_C__
#define __LCD_C__

// define the pinout.
// only required if port access is being used.
typedef struct 
{                            // This structure is overlayed
  int1 enable;          // on to an I/O port to gain
  int1 rs;              // access to the LCD pins.
  int1 rw;              // The bits are allocated from
  int1 unused;          // low order up.  ENABLE will
  unsigned int    data : 4;        // be LSB pin of that port.
  #if defined(__PCD__)      // The port used will be LCD_DATA_PORT.
  unsigned int    reserved: 8;
  #endif
} LCD_PIN_MAP;

// this is to improve compatability with previous LCD drivers that accepted
// a define labeled 'use_portb_lcd' that configured the LCD onto port B.
#if ((defined(use_portb_lcd)) && (use_portb_lcd==TRUE))
 #define LCD_DATA_PORT getenv("SFR:PORTB")
#endif

#if defined(__PCB__)
  // these definitions only need to be modified for baseline PICs.
  // all other PICs use LCD_PIN_MAP or individual LCD_xxx pin definitions.
/*                                    EN, RS,  RW,  UNUSED,  DATA  */
 const LCD_PIN_MAP LCD_OUTPUT_MAP =  {0,  0,    0,    0,      0};
 const LCD_PIN_MAP LCD_INPUT_MAP =  {0,  0,    0,    0,      0xF};
#endif

////////////////////// END CONFIGURATION ///////////////////////////////////

#ifndef LCD_ENABLE_PIN
  #define lcd_output_enable(x) lcdlat.enable=x
  #define lcd_enable_tris()  lcdtris.enable=0
#else
  #define lcd_output_enable(x) output_bit(LCD_ENABLE_PIN, x)
  #define lcd_enable_tris()  output_drive(LCD_ENABLE_PIN)
#endif

#ifndef LCD_RS_PIN
  #define lcd_output_rs(x) lcdlat.rs=x
  #define lcd_rs_tris()  lcdtris.rs=0
#else
  #define lcd_output_rs(x) output_bit(LCD_RS_PIN, x)
  #define lcd_rs_tris()  output_drive(LCD_RS_PIN)
#endif

#ifndef LCD_RW_PIN
  #define lcd_output_rw(x) lcdlat.rw=x
  #define lcd_rw_tris()  lcdtris.rw=0
#else
  #define lcd_output_rw(x) output_bit(LCD_RW_PIN, x)
  #define lcd_rw_tris()  output_drive(LCD_RW_PIN)
#endif

// original version of this library incorrectly labeled LCD_DATA0 as LCD_DATA4,
// LCD_DATA1 as LCD_DATA5, and so on.  this block of code makes the driver
// compatible with any code written for the original library
#if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3) && !defined(LCD_DATA4) && !defined(LCD_DATA5) && !defined(LCD_DATA6) && !defined(LCD_DATA7))
  #define  LCD_DATA4    LCD_DATA0
  #define  LCD_DATA5    LCD_DATA1
  #define  LCD_DATA6    LCD_DATA2
  #define  LCD_DATA7    LCD_DATA3
#endif

#ifndef LCD_DATA4
#ifndef LCD_DATA_PORT
  #if defined(__PCB__)
      #define LCD_DATA_PORT      0x06    //portb
      #define set_tris_lcd(x)  set_tris_b(x)
  #else
    #if defined(PIN_D0)
      #define LCD_DATA_PORT      getenv("SFR:PORTD")    //portd
    #else
      #define LCD_DATA_PORT      getenv("SFR:PORTB")    //portb
    #endif
  #endif 
#endif

#if defined(__PCB__)
  LCD_PIN_MAP lcd, lcdlat;
  #byte lcd = LCD_DATA_PORT
  #byte lcdlat = LCD_DATA_PORT
#elif defined(__PCM__)
  LCD_PIN_MAP lcd, lcdlat, lcdtris;
  #byte lcd = LCD_DATA_PORT
  #byte lcdlat = LCD_DATA_PORT
  #byte lcdtris = LCD_DATA_PORT+0x80
#elif defined(__PCH__)
  LCD_PIN_MAP lcd, lcdlat, lcdtris;
  #byte lcd = LCD_DATA_PORT
  #byte lcdlat = LCD_DATA_PORT+9
  #byte lcdtris = LCD_DATA_PORT+0x12
#elif defined(__PCD__)
  LCD_PIN_MAP lcd, lcdlat, lcdtris;
  #word lcd = LCD_DATA_PORT
  #word lcdlat = LCD_DATA_PORT+2
  #word lcdtris = LCD_DATA_PORT-0x02
#endif
#endif  //LCD_DATA4 not defined

#ifndef LCD_TYPE
  #define LCD_TYPE 2          // 0=5x7, 1=5x10, 2=2 lines
#endif

#ifndef LCD_LINE_TWO
  #define LCD_LINE_TWO 0x40    // LCD RAM address for the second line
#endif

#ifndef LCD_LINE_LENGTH
  #define LCD_LINE_LENGTH 16
#endif

unsigned int8 const LCD_INIT_STRING[4] = {0x20 | (LCD_TYPE << 2), 0xc, 1, 6};
                            // These bytes need to be sent to the LCD
                            // to start it up.

unsigned int8 lcd_read_nibble(void);

unsigned int8 lcd_read_byte(void)
{
  unsigned int8 low,high;

 #if defined(__PCB__)
  set_tris_lcd(LCD_INPUT_MAP);
 #else
  #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
  output_float(LCD_DATA4);
  output_float(LCD_DATA5);
  output_float(LCD_DATA6);
  output_float(LCD_DATA7);
  #else
  lcdtris.data = 0xF;
  #endif
 #endif
       
  lcd_output_rw(1);
  delay_cycles(1);
  lcd_output_enable(1);
  delay_cycles(1);
  high = lcd_read_nibble();
     
  lcd_output_enable(0);
  delay_cycles(1);
  lcd_output_enable(1);
  delay_us(1);
  low = lcd_read_nibble();
     
  lcd_output_enable(0);

 #if defined(__PCB__)
  set_tris_lcd(LCD_OUTPUT_MAP);
 #else
  #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
  output_drive(LCD_DATA4);
  output_drive(LCD_DATA5);
  output_drive(LCD_DATA6);
  output_drive(LCD_DATA7);
  #else
  lcdtris.data = 0x0;
  #endif
 #endif

  return( (high<<4) | low);
}

unsigned int8 lcd_read_nibble(void)
{
  #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
  unsigned int8 n = 0x00;

  /* Read the data port */
  n |= input(LCD_DATA4);
  n |= input(LCD_DATA5) << 1;
  n |= input(LCD_DATA6) << 2;
  n |= input(LCD_DATA7) << 3;
 
  return(n);
  #else
  return(lcd.data);
  #endif
}

void lcd_send_nibble(unsigned int8 n)
{
  #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
  /* Write to the data port */
  output_bit(LCD_DATA4, bit_test(n, 0));
  output_bit(LCD_DATA5, bit_test(n, 1));
  output_bit(LCD_DATA6, bit_test(n, 2));
  output_bit(LCD_DATA7, bit_test(n, 3));
  #else     
  lcdlat.data = n;
  #endif
     
  delay_cycles(1);
  lcd_output_enable(1);
  delay_us(2);
  lcd_output_enable(0);
}

void lcd_send_byte(unsigned int8 address, unsigned int8 n)
{
  #if defined(__PCB__)
  set_tris_lcd(LCD_OUTPUT_MAP);
  #else
  lcd_enable_tris();
  lcd_rs_tris();
  lcd_rw_tris();
  #endif

  lcd_output_rs(0);
  while ( bit_test(lcd_read_byte(),7) ) ;
  lcd_output_rs(address);
  delay_cycles(1);
  lcd_output_rw(0);
  delay_cycles(1);
  lcd_output_enable(0);
  lcd_send_nibble(n >> 4);
  lcd_send_nibble(n & 0xf);
}

#if defined(LCD_EXTENDED_NEWLINE)
unsigned int8 g_LcdX, g_LcdY;
#endif

void lcd_init(void)
{
  unsigned int8 i;

 #if defined(__PCB__)
  set_tris_lcd(LCD_OUTPUT_MAP);
 #else
  #if (defined(LCD_DATA4) && defined(LCD_DATA5) && defined(LCD_DATA6) && defined(LCD_DATA7))
  output_drive(LCD_DATA4);
  output_drive(LCD_DATA5);
  output_drive(LCD_DATA6);
  output_drive(LCD_DATA7);
  #else
  lcdtris.data = 0x0;
  #endif
  lcd_enable_tris();
  lcd_rs_tris();
  lcd_rw_tris();
 #endif

  lcd_output_rs(0);
  lcd_output_rw(0);
  lcd_output_enable(0);
   
  delay_ms(15);
  for(i=1;i<=3;++i)
  {
      lcd_send_nibble(3);
      delay_ms(5);
  }
 
  lcd_send_nibble(2);
  delay_ms(5);
  for(i=0;i<=3;++i)
      lcd_send_byte(0,LCD_INIT_STRING[i]);

  #if defined(LCD_EXTENDED_NEWLINE)
  g_LcdX = 0;
  g_LcdY = 0;
  #endif
}

void lcd_gotoxy(unsigned int8 x, unsigned int8 y)
{
  unsigned int8 address;
 
  if(y!=1)
      address=LCD_LINE_TWO;
  else
      address=0;
   
  address+=x-1;
  lcd_send_byte(0,0x80|address);

  #if defined(LCD_EXTENDED_NEWLINE)
  g_LcdX = x - 1;
  g_LcdY = y - 1;
  #endif
}

void lcd_putc(char c)
{
  switch (c)
  {
      case '\a'  :  lcd_gotoxy(1,1);    break;

      case '\f'  :  lcd_send_byte(0,1);
                    delay_ms(2);
                    #if defined(LCD_EXTENDED_NEWLINE)
                    g_LcdX = 0;
                    g_LcdY = 0;
                    #endif
                    break;

    #if defined(LCD_EXTENDED_NEWLINE)
      case '\r'  :  lcd_gotoxy(1, g_LcdY+1);  break;
      case '\n'  :
        while (g_LcdX++ < LCD_LINE_LENGTH)
        {
            lcd_send_byte(1, ' ');
        }
        lcd_gotoxy(1, g_LcdY+2);
        break;
    #else
      case '\n'  : lcd_gotoxy(1,2);        break;
    #endif
   
      case '\b'  : lcd_send_byte(0,0x10);  break;
   
    #if defined(LCD_EXTENDED_NEWLINE)
      default    :
        if (g_LcdX < LCD_LINE_LENGTH)
        {
            lcd_send_byte(1, c);
            g_LcdX++;
        }
        break;
    #else
      default    : lcd_send_byte(1,c);    break;
    #endif
  }
}
 
char lcd_getc(unsigned int8 x, unsigned int8 y)
{
  char value;

  lcd_gotoxy(x,y);
  while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  lcd_output_rs(1);
  value = lcd_read_byte();
  lcd_output_rs(0);
 
  return(value);
}

// write a custom character to the ram
// which is 0-7 and specifies which character array we are modifying.
// ptr points to an array of 8 bytes, where each byte is the next row of
//    pixels.  only bits 0-4 are used.  the last row is the cursor row, and
//    usually you will want to leave this byte 0x00.
void lcd_set_cgram_char(unsigned int8 which, unsigned int8 *ptr)
{
  unsigned int i;

  which <<= 3;
  which &= 0x38;

  lcd_send_byte(0, 0x40 | which);  //set cgram address

  for(i=0; i<8; i++)
  {
      lcd_send_byte(1, *ptr++);
  }
 
  #if defined(LCD_EXTENDED_NEWLINE)
    lcd_gotoxy(g_LcdX+1, g_LcdY+1);  //set ddram address
  #endif
}

void lcd_cursor_on(int1 on)
{
  if (on)
  {
      lcd_send_byte(0,0x0F);          //turn LCD cursor ON
  }
  else
  {
      lcd_send_byte(0,0x0C);          //turn LCD cursor OFF
  }
}

#endif
Qualquer sugestão é bem vinda!

whiplash

Mensagens : 1
Data de inscrição : 19/12/2014

Ver perfil do usuário

Voltar ao Topo Ir em baixo

Ver o tópico anterior Ver o tópico seguinte Voltar ao Topo


 
Permissão deste fórum:
Você não pode responder aos tópicos neste fórum