rocky79
Member level 5
- Joined
- Sep 20, 2010
- Messages
- 83
- Helped
- 1
- Reputation
- 2
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,903
#include <12f1840.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
#use delay(clock = 4000000)
#use fast_io (A)
//----------------------------------- ---------
//GPIO BIT ASSIGNMENT FOR I/O REGISTERS PORT A
//--------------------------------------------
//address of the TRISIO registe
#byte TRISA = 0x8C//getenv("SFR:TRISA")
#byte ANSELA = 0x18C//getenv("SFR:ANSELA")
// setting up PWM parameters
#byte PR2=0x1B
#byte T2CON=0x1C
#word CCP1CON=0x293
#bit CCP1CON_4=CCP1CON.4
#bit CCP1CON_5=CCP1CON.5
#word CCPR1L=0x291
#byte PIR1=0x11
#bit PIR1_1=PIR1.1
int16 duty;
#bit duty_0=duty.0
#bit duty_1=duty.1
void pwm(int16 a2d);
void main(void){
int16 i;
//SET UP THE CAPSENSE PINS [RA0-RA1] FOR ANALOG INPUT
ANSELA=0b00000011;
//----------------------manual PWM config--------------------------------
//Disable the PWM pin (CCPx) output driver(s) by
//setting the associated TRIS bit(s) to INPUT
TRISA= 0b00001111;
//Load the PR2 register with the PWM period value.
PR2=255; // 255hz
//Configure the CCP module for the PWM mode
//by loading the CCPxCON register with the
//appropriate values.
CCP1CON=0b00001100; // Set CCP1 PWM active high
//Load the CCPRxL register and the DCxBx bits of
//the CCPxCON register, with the PWM duty cycle
//value.
duty = 0; // set duty cycle to 0
CCP1CON_4 = duty_0; // Store duty to registers as
CCP1CON_5 = duty_1; // a 10-bit word
//store the eight MSbs in CCPR1L.
CCPR1L = duty >> 2; // store the remaining low byte
//clear the TMR21F interrupt flag bit of PIR1 register
PIR1_1=0;
// Set prescaler to 16
T2CON=0b00000110;
//Wait until Timer2 overflows, TMR2IF bit of the
//PIR1 register is set.
wait_for_TMR2IF:
if (PIR1_1 == 0)
{
goto wait_for_TMR2IF;
}
//enable PWM by setting pin to output
TRISA= 0b00001011;
while(1)
{
for(i=0;i<1020;++i)
{
//Output a PWM wave
pwm(i);
delay_ms(2);
}
delay_ms(2000);
}
}
void pwm(int16 a2d)
{
duty=a2d;
CCP1CON_4 = duty_0; // Store duty to registers as
CCP1CON_5 = duty_1; // a 10-bit word
CCPR1L = duty >> 2; // store the remaining low byte}
}
This is to slow down the PWM ramp up.
It is an unusual procedure.
By the way, had you used PIC Wizard tool which come with CCS toolset to get the values that you configured for PWM registers ?
+++
//Wait until Timer2 overflows, TMR2IF bit of the
//PIR1 register is set.
wait_for_TMR2IF:
if (PIR1_1 == 0)
{
go
#byte PIR1=0x11
#bit PIR1_1=PIR1.1
//clear the TMR21F interrupt flag bit of PIR1 register
PIR1_1=0;
void main(void)
{
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 255, 1);
int16 i;
while( 1 )
{
for(i=0;i<1020;++i)
{
//Output a PWM wave
set_pwm1_duty(i);
delay_ms(3);
}
delay_ms(500);
}
}
.................... {
.................... setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
0021: BCF 1D.0
0022: BCF 1D.1
0023: MOVLB 01
0024: BCF 0C.2
0025: MOVLB 02
0026: BCF 0C.2
0027: MOVLW 0C
0028: MOVLB 05
0029: MOVWF 13
002A: CLRF 14
002B: CLRF 15
002C: MOVLW 01
002D: MOVWF 16
.................... setup_timer_2(T2_DIV_BY_16, 255, 1);
002E: MOVLW 00
002F: MOVLB 00
0030: MOVWF 21
0031: IORLW 06
0032: MOVWF 1C
0033: MOVLW FF
0034: MOVWF 1B
.................... {
.................... //Output a PWM wave
.................... set_pwm1_duty(i);
0041: MOVF 26,W
0042: MOVWF 22
0043: MOVF 25,W
0044: MOVWF 21
0045: RRF 22,F
0046: RRF 21,F
0047: RRF 22,F
0048: RRF 21,F
0049: RRF 22,F
004A: MOVF 21,W
004B: MOVLB 05
004C: MOVWF 11
004D: MOVLB 00
004E: RRF 22,F
004F: RRF 22,W
0050: ANDLW 30
0051: MOVWF 20
0052: MOVLB 05
0053: MOVF 13,W
0054: ANDLW CF
0055: MOVLB 00
0056: IORWF 20,W
0057: MOVLB 05
0058: MOVWF 13
0023: MOVLB 01
0024: BCF 0C.2
0025: MOVLB 02
0026: BCF 0C.2
0027: MOVLW 0C
0028: MOVLB 05
0029: MOVWF 13
002A: CLRF 14
002B: CLRF 15
002C: MOVLW 01
002D: MOVWF 16
.................... {
.................... setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
0021: BCF 1D.0
0022: BCF 1D.1
#include <12f1840.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
#use delay(clock = 4000000)
//#use fast_io (A)
void main(void)
{
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_16, 255, 1);
int16 i;
while(1)
{
for(i=0;i<1020;++i)
{
//Output a PWM wave
set_pwm1_duty(i);
delay_ms(3);
}
delay_ms(500);
}
}
ROM used: 114 words (3%)
Largest free fragment is 2048
RAM used: 6 (2%) at main() level
24 (9%) worst case
Stack: 1 locations
*
0000: MOVLP 00
0001: GOTO 019
0002: NOP
.................... #include <12f1840.h>
.................... //////// Standard Header file for the PIC12F1840 device ////////////////
.................... #device PIC12F1840
.................... #list
....................
.................... #fuses INTRC_IO, NOWDT, NOPROTECT, NOMCLR
.................... #use delay(clock = 4000000)
0003: MOVLW 20
0004: MOVWF 05
0005: MOVLW 08
0006: MOVWF 04
0007: MOVF 00,W
0008: BTFSC 03.2
0009: GOTO 018
000A: MOVLW 01
000B: MOVWF 21
000C: CLRF 20
000D: DECFSZ 20,F
000E: GOTO 00D
000F: DECFSZ 21,F
0010: GOTO 00C
0011: MOVLW 4A
0012: MOVWF 20
0013: DECFSZ 20,F
0014: GOTO 013
0015: GOTO 016
0016: DECFSZ 00,F
0017: GOTO 00A
0018: RETURN
.................... //#use fast_io (A)
....................
....................
.................... void main(void)
.................... {
0019: CLRF 05
001A: CLRF 04
001B: MOVLW 1F
001C: ANDWF 03,F
001D: MOVLW 6A
001E: MOVLB 01
001F: MOVWF 19
0020: MOVLB 03
0021: CLRF 0C
0022: MOVLB 02
0023: CLRF 12
0024: CLRF 11
0025: CLRF 14
0026: CLRF 13
.................... setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
0027: BCF 1D.0
0028: BCF 1D.1
0029: MOVLB 01
002A: BCF 0C.2
002B: MOVLB 02
002C: BCF 0C.2
002D: MOVLW 0C
002E: MOVLB 05
002F: MOVWF 13
0030: CLRF 14
0031: CLRF 15
0032: BCF 1E.0
0033: BCF 1E.1
0034: MOVLW 01
0035: MOVWF 16
.................... setup_timer_2(T2_DIV_BY_16, 255, 1);
0036: MOVLW 00
0037: MOVLB 00
0038: MOVWF 21
0039: IORLW 06
003A: MOVWF 1C
003B: MOVLW FF
003C: MOVWF 1B
.................... int16 i;
....................
.................... while(1)
.................... {
.................... for(i=0;i<1020;++i)
003D: CLRF 26
003E: CLRF 25
003F: MOVF 26,W
0040: SUBLW 03
0041: BTFSS 03.0
0042: GOTO 069
0043: BTFSS 03.2
0044: GOTO 049
0045: MOVF 25,W
0046: SUBLW FB
0047: BTFSS 03.0
0048: GOTO 069
.................... {
.................... //Output a PWM wave
.................... set_pwm1_duty(i);
0049: MOVF 26,W
004A: MOVWF 22
004B: MOVF 25,W
004C: MOVWF 21
004D: RRF 22,F
004E: RRF 21,F
004F: RRF 22,F
0050: RRF 21,F
0051: RRF 22,F
0052: MOVF 21,W
0053: MOVLB 05
0054: MOVWF 11
0055: MOVLB 00
0056: RRF 22,F
0057: RRF 22,W
0058: ANDLW 30
0059: MOVWF 20
005A: MOVLB 05
005B: MOVF 13,W
005C: ANDLW CF
005D: MOVLB 00
005E: IORWF 20,W
005F: MOVLB 05
0060: MOVWF 13
.................... delay_ms(3);
0061: MOVLW 03
0062: MOVLB 00
0063: MOVWF 28
0064: CALL 003
.................... }
0065: INCF 25,F
0066: BTFSC 03.2
0067: INCF 26,F
0068: GOTO 03F
.................... delay_ms(500);
0069: MOVLW 02
006A: MOVWF 27
006B: MOVLW FA
006C: MOVWF 28
006D: CALL 003
006E: DECFSZ 27,F
006F: GOTO 06B
.................... }
0070: GOTO 03D
.................... }
.................... {
.................... setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
0021: BCF 1D.0
0022: BCF 1D.1
puts 06 in T2CON.................... setup_timer_2(T2_DIV_BY_16, 255, 1);
0036: MOVLW 00
0037: MOVLB 00
0038: MOVWF 21
0039: IORLW 06
003A: MOVWF 1C
003B: MOVLW FF
003C: MOVWF 1B
.................... setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
0027: BCF 1D.0
0028: BCF 1D.1
0029: MOVLB 01
002A: BCF 0C.2
002B: MOVLB 02
002C: BCF 0C.2
002D: MOVLW 0C
002E: MOVLB 05
002F: MOVWF 13
0030: CLRF 14
0031: CLRF 15
0032: BCF 1E.0
0033: BCF 1E.1
0034: MOVLW 01
0035: MOVWF 16
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?