vsmGuy said:Use a freq. divider!
blueroomelectronics said:Too slow at that clock as you've already discovered.
blueroomelectronics said:5Hz is possible with a 32kHz crystal.
//use a counter to help adjust timing
#include <htc.h>
__CONFIG (MCLRDIS & UNPROTECT & BORDIS & WDTDIS & INTIO & PWRTEN);
//hardware configuration
#define nLED 2 //LED on gpio2
#define MaxCnt 122 //maximum count to flip the LED
unsigned char sGPIO; //shadow gpio
void interrupt LED(void) {
static unsigned char T0Counter=MaxCnt; //initialize t0 counter. "static" to retain value
T0IF=0; //clear the interrupt flag
T0Counter-=1;
if (T0Counter==0) {
T0Counter=MaxCnt;
sGPIO ^=(1<<nLED); //flash nLED pin
}
}
void
main(void)
{
TRISIO=~(1<<nLED); //all gpio pins to input other than nLED
OPTION=0b11010100; //setting up the options register;
// 1 //GPPU: pull-up disabled
// 1 //INTEDG: interrupt on rising edge
// 0 //T0CS: timer0 source select - internal clock cycle
// 1 //T0SE: timer0 edge select - interrupt on high-to-low transisiton
// 0 //PSA: prescaler selected to timer0
// 100 //PS0..2: prescaler rate select - 100=32.
//GPPU=1; INTEDG=1;T0CS=0;T0SE=1;PSA=0;
sGPIO=0; //clear gpio pins
T0IE=1; //enable timer 0 interrupt
ei(); //enable global interrupt. = GIE=1;
while (1){
//TODO Auto-generated main function
GPIO=sGPIO;
}
}
#define MaxCnt 1220 //maximum count to flip the LED
static unsigned long int T0Counter=MaxCnt; //initialize t0 counter. "static" to retain value
I am using assembly language for programming the PIC
In my opinion, you can either decide to use the more convenient C programming or need to have enough time to understand how to do in assembly.but I just don't have enough time
Time equ 0x62
PulseState equ 0x63
TimeOn equ 0x64 ;----last register
org 0x00
goto start
;___________________________________________________________________
;_____________________________________INTERRUPT_____________________
org 0x000018
;___________________________________________________________________
;****************************************PWM************************
decf Time,f
btfsc STATUS,Z ;IS PERIOD ZERO?
goto ISR ;YES
goto PWM_Exit
ISR
comf PulseState,f ;TEST PREVIOUS STATE OF OUTPUT pwm PIN
btfsc STATUS,Z
goto Go_Low ;IT WAS HIGH
Go_high ;IT WAS LOW
movlw .1 ;Test if TimeOn is zero(i.e one)
subwf TimeOn,w
btfsc STATUS,Z
goto Go_high_exit
bsf PORTB,7 ;Go High
Go_high_exit
movfw TimeOn
movwf Time
goto PWM_Exit
;---
Go_Low
movfw TimeOn ;Test if TimeOn is 13(i.e one)
sublw .13
btfsc STATUS,Z
goto Go_low_exit
bcf PORTB,7 ;Go Low
Go_low_exit
movfw TimeOn
sublw .14
movwf Time
goto PWM_Exit
PWM_Exit
bcf PIR1,0 ;CLEAR FLAG
RETFIE
;****************************INITIALIZE******************
start
; Initialize PORTs
; :
; :
; :
goto main
;_______________________________________________________
;**********************************MAIN*****************
;_______________________________________________________
main
bsf PORTC,7
bcf PORTB,7
movlw .0
movwf PulseState
movlw .4
movwf TimeOn
movlw .1
movwf Time
clrf PIR1
bsf INTCON,GIE
bsf INTCON,PEIE
Bank1
bsf PIE1,0 ;ENABLE TMR1 INTERRUPT
Bank0
movlw b'00110100'
movwf T1CON
bsf T1CON,TMR1ON
infinite
goto infinite
end
//use a counter to help adjust timing
#include <htc.h>
__CONFIG (MCLRDIS & UNPROTECT & BORDIS & WDTDIS & INTIO & PWRTEN);
//hardware configuration
#define nLED 2 //LED on gpio2
//#define MaxCnt 122 //maximum count to flip the LED
#define CntTotal 200000/2/256/1 //maximum count of interrupts during an entire pwm cycle: 200,000us pwm cycle, (1us*256 cycles per interrupt, and 1/2x prescaler)
#define CntON 100 //maximum count to keep the LED on
#define CntOFF (CntTotal-CntON) //maximum count to keep the LED off
unsigned long int MaxCnt=CntOFF;
unsigned long int T0Counter=CntOFF;
unsigned char ON_OFF_Flag=0; //flag for on / off states. 0=off, 1=on;
unsigned char sGPIO; //shadow gpio
void interrupt LED(void) {
//static unsigned long int T0Counter=MaxCnt; //initialize t0 counter. "static" to retain value
T0IF=0; //clear the interrupt flag
T0Counter-=1;
if (T0Counter==0) { //time to toggle the output
if (ON_OFF_Flag==0) {ON_OFF_Flag=1; MaxCnt=CntON;}
else {ON_OFF_Flag=0; MaxCnt=CntOFF;}
T0Counter=MaxCnt; //reset T0Counter
sGPIO ^=(1<<nLED); //flash nLED pin
}
}
void
main(void)
{
TRISIO=~(1<<nLED); //all gpio pins to input other than nLED
OPTION=0b11010000; //setting up the options register;
// 1 //GPPU: pull-up disabled
// 1 //INTEDG: interrupt on rising edge
// 0 //T0CS: timer0 source select - internal clock cycle
// 1 //T0SE: timer0 edge select - interrupt on high-to-low transisiton
// 0 //PSA: prescaler selected to timer0
// 000 //PS0..2: prescaler rate select - 100=32. 010 = 8. 000 = 2
//GPPU=1; INTEDG=1;T0CS=0;T0SE=1;PSA=0;
sGPIO=0; //clear gpio pins
T0IE=1; //enable timer 0 interrupt
ei(); //enable global interrupt. = GIE=1;
while (1){
//TODO Auto-generated main function
GPIO=sGPIO;
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?