CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

12F1822 PWM problem
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 08, 2021 8:52 am     Reply with quote

ads wrote:
Hi. Sorry for late reply. My compiler version is 5.008 and I found it if I write the code as
Code:
#use pwm (PWM1, FREQUENCY = 38KHz, DUTY = 25)

(by deleting last command which is PWM_OFF then it compiles. But I don't know if it will work? Do you have any idea the whole code is:

Code:
// Extended NEC protocol IR remote control transmitter CCS C code
 
#include <12F1822.h>
#fuses NOMCLR INTRC_IO PLL_SW
#use delay(clock=32000000)
#use fast_io(A)
#use pwm (PWM1, FREQUENCY = 38KHz, DUTY = 25, PWM_OFF)
 
void send_signal(unsigned int32 number){
  int8 i;
  // Send 9ms burst
  pwm_on();
  delay_ms(9);
  // Send 4.5ms space
  pwm_off();
  delay_us(4500);
  // Send data
  for(i = 0; i < 32; i++){
    // If bit is 1 send 560us pulse and 1680us space
    if(bit_test(number, 31 - i)){
      pwm_on();
      delay_us(560);
      pwm_off();
      delay_us(1680);
    }
    // If bit is 0 send 560us pulse and 560us space
    else{
      pwm_on();
      delay_us(560);
      pwm_off();
      delay_us(560);
    }
  }
  // Send end bit
  pwm_on();
  delay_us(560);
  pwm_off();
  delay_us(560);
}
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);  // Set internal oscillator to 32MHz (8MHz and PLL)
  output_a(0);
  set_tris_a(0x3B);   // Configure RA2 pin as output and others as inputs
  port_a_pullups(0x3B);  // Enable internal pull-ups for pins RA0,RA1,RA3,RA4 & RA5
  while(TRUE){
    while(!input(PIN_A0)){
      send_signal(0x40BF00FF);
      delay_ms(500);
    }
    while(!input(PIN_A1)){
      send_signal(0x40BF807F);
      delay_ms(500);
    }
    while(!input(PIN_A3)){
      send_signal(0x40BF40BF);
      delay_ms(500);
    }
    while(!input(PIN_A4)){
      send_signal(0x40BF20DF);
      delay_ms(500);
    }
    while(!input(PIN_A5)){
      send_signal(0x40BFA05F);
      delay_ms(500);
    }
  }
}



from https://simple-circuit.com/ir-remote-control-transmitter-receiver-ccs-c/



I’m having same problem with code as-is,
It may have worked using an older version of CCS, but not with newest, which I have. I’ll try some of the suggestions quoted here. Thanks

I’m a newby BtW so I’m still learning.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 08, 2021 6:29 pm     Reply with quote

I tested this in hardware with the code you posted and it's putting out
pulses on pin A2 (pin 5 on the DIP8 PIC package). That's correct.
I tested this on a Microchip Low Pin Count board with a 12F1822 and
CCS vs. 5.103.

To measure the frequency, I modified the main() by adding the two lines
shown in bold below. I got a freq of 37.91 on my scope with 25% duty
cycle. That's correct.
Quote:
void main() {
setup_oscillator(OSC_8MHZ | OSC_PLL_ON);
output_a(0);
set_tris_a(0x3B);
port_a_pullups(0x3B);

pwm_on();
while(TRUE);


I don't have an NEC TV to test it with. If you look at his while() loop,
he has buttons on pins A0, A1, A3, A4, A5. These buttons connect ground
to the PIC pins if they are pressed. The pullups are provided by the PIC
as shown in his code above.

You will need this button circuit on each of the 5 pins listed above:
Code:

           ___  Pushbutton Switch 
To        _|_|_
PIC ------o   o------
pin                 |             
                   --- GND
                    -
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 08, 2021 10:38 pm     Reply with quote

PCM programmer wrote:
I tested this in hardware with the code you posted and it's putting out
pulses on pin A2 (pin 5 on the DIP8 PIC package). That's correct.
I tested this on a Microchip Low Pin Count board with a 12F1822 and
CCS vs. 5.103.

To measure the frequency, I modified the main() by adding the two lines
shown in bold below. I got a freq of 37.91 on my scope with 25% duty
cycle. That's correct.
Quote:
void main() {
setup_oscillator(OSC_8MHZ | OSC_PLL_ON);
output_a(0);
set_tris_a(0x3B);
port_a_pullups(0x3B);

pwm_on();
while(TRUE);


I don't have an NEC TV to test it with. If you look at his while() loop,
he has buttons on pins A0, A1, A3, A4, A5. These buttons connect ground
to the PIC pins if they are pressed. The pullups are provided by the PIC
as shown in his code above.

You will need this button circuit on each of the 5 pins listed above:
Code:

           ___  Pushbutton Switch 
To        _|_|_
PIC ------o   o------
pin                 |             
                   --- GND
                    -


Thanks, I followed some of your suggestions, by adding them to the MAIN:



setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,209,1);

Got a successful build, 0=errors,0=warnings


I’ll program tomorrow and try it out. I’m also working on the RX as well.

I build audio equipment for a living and I’m wanting to build a remote system for a preamplifier, I thought this would be a good solution.
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Mon Mar 08, 2021 10:42 pm     Reply with quote

PCM programmer wrote:
I tested this in hardware with the code you posted and it's putting out
pulses on pin A2 (pin 5 on the DIP8 PIC package). That's correct.
I tested this on a Microchip Low Pin Count board with a 12F1822 and
CCS vs. 5.103.

To measure the frequency, I modified the main() by adding the two lines
shown in bold below. I got a freq of 37.91 on my scope with 25% duty
cycle. That's correct.
Quote:
void main() {
setup_oscillator(OSC_8MHZ | OSC_PLL_ON);
output_a(0);
set_tris_a(0x3B);
port_a_pullups(0x3B);

pwm_on();
while(TRUE);


I don't have an NEC TV to test it with. If you look at his while() loop,
he has buttons on pins A0, A1, A3, A4, A5. These buttons connect ground
to the PIC pins if they are pressed. The pullups are provided by the PIC
as shown in his code above.

You will need this button circuit on each of the 5 pins listed above:
Code:

           ___  Pushbutton Switch 
To        _|_|_
PIC ------o   o------
pin                 |             
                   --- GND
                    -



Do you use the low or high power IR emitters? just curious.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 12:43 am     Reply with quote

Denny9167 wrote:

I’m having same problem with code as-is,
It may have worked using an older version of CCS, but not
with newest, which I have.

I followed some of your suggestions, by adding them to the MAIN:

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,209,1);

Got a successful build, 0=errors,0=warnings

If you had to add those two lines to get a successful build, then you don't
have the newest version of the compiler, which is vs. 5.103. You can see
the version that you have by looking at the top of the .LST file.
Code:

CCS PCM C Compiler, Version 5.103, xxxxx       08-Mar-21 22:42

Filename:   C:\...\Projects\PCM_Test\PCM_TEST.lst

               ROM used:   295 words (14%)
                           Largest free fragment is 1753
               RAM used:   6 (5%) at main() level
                           23 (18%) worst case
               Stack used: 2 locations
               Stack size: 16

I didn't change anything in the code you posted, and it compiled fine with
vs. 5.103. Here is a link to your post:
http://www.ccsinfo.com/forum/viewtopic.php?t=58717&start=8
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 12:44 am     Reply with quote

Denny9167 wrote:

Do you use the low or high power IR emitters? just curious.

I have not assembled a board. I am just helping you with the code, and
the PWM operation of the PIC. I don't know which IR emitter is best.
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 09, 2021 5:59 am     Reply with quote

PCM programmer wrote:
Denny9167 wrote:

Do you use the low or high power IR emitters? just curious.

I have not assembled a board. I am just helping you with the code, and
the PWM operation of the PIC. I don't know which IR emitter is best.


Appreciate it very much!
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Tue Mar 09, 2021 9:26 am     Reply with quote

PCM programmer wrote:
Denny9167 wrote:

I’m having same problem with code as-is,
It may have worked using an older version of CCS, but not
with newest, which I have.

I followed some of your suggestions, by adding them to the MAIN:

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,209,1);

Got a successful build, 0=errors,0=warnings

If you had to add those two lines to get a successful build, then you don't
have the newest version of the compiler, which is vs. 5.103. You can see
the version that you have by looking at the top of the .LST file.
Code:

CCS PCM C Compiler, Version 5.103, xxxxx       08-Mar-21 22:42

Filename:   C:\...\Projects\PCM_Test\PCM_TEST.lst

               ROM used:   295 words (14%)
                           Largest free fragment is 1753
               RAM used:   6 (5%) at main() level
                           23 (18%) worst case
               Stack used: 2 locations
               Stack size: 16

I didn't change anything in the code you posted, and it compiled fine with
vs. 5.103. Here is a link to your post:
http://www.ccsinfo.com/forum/viewtopic.php?t=58717&start=8


I have v5.103, just purchased it last weekend. Anyway I’m going to try and purchase a logic analyzer as well to help in testing. I hope you’ll be patient with me, I’m an old analog guy, work mostly with vacuum tubes, so much of this digital stuff is new to me, I have learned a lot in the last few months though.


Last edited by Denny9167 on Tue Mar 09, 2021 11:02 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 9:30 am     Reply with quote

Well done,
5.008, was a bit of an 'ouch'. V5, was 'beta at best' at that point, which
explains a lot of the problems.
Hopefully things will move forwards a little now. Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Mar 09, 2021 11:53 am     Reply with quote

My advice is to look at the .LST file to confirm that vs. 5.103 was installed
and is being used to compile code.

Make sure that vs. 5.008 was uninstalled, or at least is not active.

- Press the Start button in Windows
-- Click on Programs
--- Click on PIC-C
---- Click on Compiler Versions - it should say 5.103
----- Click the Other Versions button - Is 5.008 still installed ?

If it shows 5.008 is still there, it will give you the option to remove it.
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 10, 2021 4:36 am     Reply with quote

I read this article from CCS, so apparently PWM setup is a simple as using
The one line preprocessor directive #use pwm(). Correct?

https://www.ccsinfo.com/newsdesk_info.php?newsPath=ALL&newsdesk_id=182
Denny9167



Joined: 15 Feb 2021
Posts: 49

View user's profile Send private message Send e-mail

PostPosted: Wed Mar 10, 2021 4:46 am     Reply with quote

It seems the directive, “PWM_OFF” in #use , needs to be changed to “disable_level=low” instead??

So that when “pwm_off” function is called , the pins
Being used, are driven low?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 10, 2021 9:48 pm     Reply with quote

Denny9167 wrote:
I read this article from CCS, so apparently PWM setup is a simple as using
The one line preprocessor directive #use pwm(). Correct?

https://www.ccsinfo.com/newsdesk_info.php?newsPath=ALL&newsdesk_id=182

Yes, and then you can use these functions to change the PWM:

pwm_set_duty_percent()
pwm_set_frequency()
pwm_set_duty()
pwm_on()
pwm_off()

The program you posted uses this method.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 10, 2021 10:28 pm     Reply with quote

Denny9167 wrote:
It seems the directive, “PWM_OFF” in #use, needs to be changed to “disable_level=low” instead??

I don't see that parameter listed in the CCS manual for #use pwm()
or in the ccsc.chm help file for vs. 5.103.
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Thu Mar 11, 2021 1:52 am     Reply with quote

You seem to be trying to make this difficult.

Understand that when 'PWM_OFF' is selected, the actual peripheral
is disabled. The level the line then goes to depends on what output
instructions have latched to the registers.

The reason it is not compiling with PWM_OFF, is simple. Your compiler
version did not support this option. Simply look in the manual with
your compiler. You will find the #USE PWM, does not list any such option.

Much simpler to let the PWM drive the pin:

Just use:

#use pwm (PWM1, FREQUENCY = 38KHz, DUTY = 0)

This then enables the PWM peripheral, with the output set low. Just
what you want.

Then when you want to enable/disable the PWM, instead use:

set_pwm_duty_percent(25); //will enable the PWM output with 25% duty.

and

set_pwm_duty_percent(0); //will set the output low.

You have to use the options that are actually available with the compiler
you have.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group