void setPWMDuty(unsigned char percent){ unsigned int duty; duty = (percent * maxDuty)/100; CCPR1L = duty >> 2; // Set MSB values CCP1CON = ((duty & 3) << 4) | 0x0C; // set initial duty and set PWM mode bits.}
char msg[50]=" PICkit2 Electronicslab.ph", a, b, num_chars; //meron leading spaces para pag on blanko munavoid scroll(void){for(a = 0;a < num_chars;a++){//num_chars galing sa strlen()set_address(0x00); //sa unang linya ng LCDfor(b = 0;b < 16;b++) //kung ilan chars ng LCD, dito 16lcd_char(msg[b + c]) //show charsdelay_ms(300); //scroll speed}}
bigatin pala compiler mo master zerowing,eto pala contribution ko sa thread, galing sa LCD scrolling messageibaibang coding style ibaibang compiler basically ganito lang magpascroll sa LCDCode: [Select]char msg[50]=" PICkit2 Electronicslab.ph", a, b, num_chars; //meron leading spaces para pag on blanko munavoid scroll(void){for(a = 0;a < num_chars;a++){//num_chars galing sa strlen()set_address(0x00); //sa unang linya ng LCDfor(b = 0;b < 16;b++) //kung ilan chars ng LCD, dito 16lcd_char(msg[b + c]) //show charsdelay_ms(300); //scroll speed}}
di ko masundan sir Rob... di ko masundana ang linyang ito:lcd_char(msg[b + c]) //show charsmarahil msg[a+b]
pa-request naman ng routines for software SPI.. like "spi_write(address, byte);" and "spi_read(address);"
unsigned char spi_write(unsigned char data){ unsigned char v, i; v = 0; i = 8; CLK = 1; do { if (data & 0b10000000) { MOSI = 1; } else { MOSI = 0; } bit_delay(); CLK = 0; v <<= 1; if (MISO = 1) v |= 1; data <<= 1; CLK = 1; } while (--i); return v;}
thanks, sir zer0! gusto ko sana yan i-apply sa memory card interfacing (SD card in SPI mode).compatible ba yan sa physical layer ng memory card interfacing?
ang gusto ko po sanang matutunan using hi tech c ay ang pag gamit ng comparator ng PIC.
#include <pic.h>#include "pic16f_comparator.h"__CONFIG(HS & MCLRDIS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);void main(){ TRISA &= ~0x18; init_comparator(); while(1) { //does nothing }}
//macro#define COMP_2IN_COM_REF_OUT 0x06//function prototypevoid init_comparator(void);
#include <pic.h>#include "pic16f_comparator.h"void init_comparator(void){ C2INV = 0; //comparator 2 output not inverted C1INV = 0; //comparator 1 output not inverted CMCON &= ~0x07; //clear CM2:CM0 bits CMCON |= COMP_2IN_COM_REF_OUT; //mode: Two common reference //comparator with output return;}
here is a sample code for comparator using PIC16F88. There are 8 possible comparator modes; this sample program uses mode 6 (Two common reference comparator with output). Refer to datasheet for more details on comparator operation.main.cCode: [Select]#include <pic.h>#include "pic16f_comparator.h"__CONFIG(HS & MCLRDIS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);void main(){ TRISA &= ~0x18; init_comparator(); while(1) { //does nothing }}pic16f_comparator.hCode: [Select]//macro#define COMP_2IN_COM_REF_OUT 0x06//function prototypevoid init_comparator(void);pic16f_comparator.cCode: [Select]#include <pic.h>#include "pic16f_comparator.h"void init_comparator(void){ C2INV = 0; //comparator 2 output not inverted C1INV = 0; //comparator 1 output not inverted CMCON &= ~0x07; //clear CM2:CM0 bits CMCON |= COMP_2IN_COM_REF_OUT; //mode: Two common reference //comparator with output return;}to test the above code, connect 3 potentiometers to RA0, RA1, and RA2 and 2 LEDs to RA4 and RA3.During program execution, if analog voltage at RA1 is lesser than analog voltage at RA2, RA4 will be logic 1 (LED1 on).If analog voltage at RA0 is lesser than analog voltage at RA2, RA3 will be logic 1 (LED2 on).
Dear folks, since many members are requesting for PIC codes, let this thread be our main repository for C codes written for any C compiler (hitech, ccs c, mikroc, sdcc, c18.. ).Let me startHere is a simple program. You can simulate using Pr*teus Code: [Select]//output a 10khz squarewave, 25% duty cycle signal at RC2/CCP1 pin//PIC16F877 with 4 mhz clock#include <pic.h>__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT);void init_PWM(void){ //clear TMR2 relevant registers CCP1CON = 0x00; TMR2 = 0x00; //clear TMR2 register TMR2ON = 0; //TMR2 is off //configure TMR2 in PWM mode CCP1CON = 0x0C; //PWM mode T2CON &= ~0x03; //set prescaler, 1:1 PR2 = 0x60; //set period/frequency, ~10khz CCPR1L = 0x18; //set duty cycle, ~40khz CCP1CON &= ~0x30 ; //PWM output pin TRISC2 = 0; //disable interrupts PEIE = 0; GIE = 0; //enable TMR2 T2CON = 1; //PWM signal is generated. //You can view the square wave signal //using an oscilloscope, probe connected to //RC2 pin}void main(){ init_PWM(); while(1);}
//output a 10khz squarewave, 25% duty cycle signal at RC2/CCP1 pin//PIC16F877 with 4 mhz clock#include <pic.h>__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT);void init_PWM(void){ //clear TMR2 relevant registers CCP1CON = 0x00; TMR2 = 0x00; //clear TMR2 register TMR2ON = 0; //TMR2 is off //configure TMR2 in PWM mode CCP1CON = 0x0C; //PWM mode T2CON &= ~0x03; //set prescaler, 1:1 PR2 = 0x60; //set period/frequency, ~10khz CCPR1L = 0x18; //set duty cycle, ~40khz CCP1CON &= ~0x30 ; //PWM output pin TRISC2 = 0; //disable interrupts PEIE = 0; GIE = 0; //enable TMR2 T2CON = 1; //PWM signal is generated. //You can view the square wave signal //using an oscilloscope, probe connected to //RC2 pin}void main(){ init_PWM(); while(1);}