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 support@ccsinfo.com

Controlling the Brightness of the Back-Light [Solved]
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gaugeguy



Joined: 05 Apr 2011
Posts: 288

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 5:43 am     Reply with quote

I have not seen new, corrected code since it was pointed out to you by PCM programmer that you cannot put 624 into a value requiring an 8 bit integer.
Another mistake in the code you posted is putting values into set_pwmx_duty() that are larger than a 10 bit integer (1024).
Using values larger that the function expects can result in unpredictable output.
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 7:44 am     Reply with quote

Lets step through some things.

First:

enable_interrupts(INT_RDA);
enable_interrupts(INT_RDA2);

You must _never_ enable an interrupt for which a handler does not exist.
Doing so can prevent the code from operating.

Then you have already had comments made about the PR2 value in:

setup_timer_2(T2_DIV_BY_16,624,1);

The value here is 255 maximum.

Then there have been comments about the duty values, but in fact these
depend on what you select for the PR2 value. The maximum duty value
is (PR2+1)*4. So if you put 100 into PR2, the full on value will be 404.

Then you do not show us how 'pwm_value' is declared. This needs to be
an int16.

Then you talk about pin E0, but you are routing CCP3 to pin E1.

The data sheet is _essential_ reading. Look in this and you will see that
PWM3B is pin E1. You are routing this PWM to the B output.

You are also routing the output to a pin that it does not expect to arrive at.
Hence the TRIS for this needs to be set to zero.

So:
Code:

    setup_timer_2(T2_DIV_BY_4,255,1);
    setup_ccp3(CCP_PWM|CCP_PULSE_STEERING_A);
    output_drive(PIN_E0);
    set_pwm3_duty(512L);


Is the sequence to give a 50% PWM output at 3906Hz on pin E0.
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Thu Apr 15, 2021 9:33 am     Reply with quote

Hello gaugeguy and PCM Programmer, nothing went right for what I saw in my study books. So I looked on the internet for examples of PWM code for LED, outside the CCS forum, I found on the internet then a program for calculating PWM with the CCS code ready and then this application has an error, and I didn't notice that "period" was longer than an 8-bit integer, because I'm new to programming and it went unnoticed.

don't worry about enable_interrupts (INT_RDA2); already commented // removed

I did not see in the CCS manual talking about the instruction CCP_PULSE_STEERING_A, that CCP_PULSE_STEERING_A refers to PIN_E0 that CCP_PULSE_STEERING_B refers to PIN_E1. I only saw in the datasheet that the PWM of the 18f46k22 has direction control.

Come on.
[code]
setup_timer_2 (T2_DIV_BY_4,255.1);
setup_ccp3 (CCP_PWM | CCP_PULSE_STEERING_A);
output_drive (PIN_E0);
set_pwm3_duty (512L);
[/ code]

I took this example and also, I will try to generate the 400Hz for an LED.

Never seen about output_drive (PIN_E0)


See in the image of the LCD what happens to the characters when I use CCP_PULSE_STEERING_A the characters are scrambled, but the firmware works, except the LCD.

I'm sorry for the lack of knowledge, I already thought about leaving PWM for now and moving again in a few days.

I designed a didactic PCB that has everything, I just hadn't practiced PWM, and I'm stuck in it, it doesn't seem like it, but it is as difficult as it was with a circular buffer matrix.
_________________
Gradually you will go far with persistence, will and determination!
temtronic



Joined: 01 Jul 2010
Posts: 9113
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 9:50 am     Reply with quote

Can you post schematic of PCB as well as code for the LCD ?

I've used both 46K22 and LCds for years , never had your problem....
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Thu Apr 15, 2021 9:54 am     Reply with quote

temtronic wrote:
Can you post schematic of PCB as well as code for the LCD ?

I've used both 46K22 and LCds for years , never had your problem....


Hello TENTRONIC. I did not do the scheme, I do the PCB directly.

But the LCD is in port B, using 8 bits.
En = PIN_D4
cs = PIN_D5
it helps?

But everything works perfectly, I only have this problem when I use this line:
setup_ccp3 (CCP_PWM | CCP_PULSE_STEERING_A);

if you use:
setup_ccp3 (CCP_PWM | CCP_PULSE_STEERING_B);
the LCD works normally

But I can provide a basic scheme for you to know what is on each pin.
_________________
Gradually you will go far with persistence, will and determination!
temtronic



Joined: 01 Jul 2010
Posts: 9113
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 10:05 am     Reply with quote

I think(haven't tried) that you're using Enhanced PWM mode and not 'standard PWM'. Standard only has 1 CCPx output, not a choice of 1-4 outputs.
It may be a compiler 'bug'..others who use it ,may be able to confirm the correct setup,though post your compiler version.....
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 10:10 am     Reply with quote

The point about output_drive, is the PWM output needs the TRIS enabled.
The compiler knows to do this on the standard PWM pin, but not if you
use the pulse steering.
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Thu Apr 15, 2021 11:03 am     Reply with quote

Code that I tried last, even though I didn't do the calculation for 400Hz. a minimum filament on the led should light up. Even so, I have no value ...Crying or Very sad

ccs 5,076

Code:

#include <18f46k22.h>
#use delay(internal=16M)

#fuses INTC_IO,PUT,BROWNOUT,NOLVP,NODEBUG,NOMCLR,NOXINST,NOPBADEN,NOIESO,NOFCMEN,

#use rs232(baud=9600, parity=N, xmit=PIN_D6, rcv=PIN_D7, bits=8, stop=1, ERRORS, stream=ready)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stop=1, ERRORS, stream=PC)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)

#bit tmr0if = 0xff2.2

#use standard_io (a)
#use standard_io (b)
#use standard_io (c)
#use standard_io (d)
#use fast_io     (e) 

#include "display_8bits18f46k22-MD401.c"


void main()
{
    setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256);
    setup_timer_1(T1_DISABLED);
    set_timer0(11);
     
    enable_interrupts(GLOBAL);

    enable_interrupts(INT_RDA);
   
    setup_adc_ports(NO_ANALOGS);
    setup_adc(ADC_OFF);
    setup_comparator(NC_NC_NC_NC);
    setup_vref(FALSE);
 
    set_tris_e(0b00001100);
    output_e(0b00000000);
   
    // setup pwm ///////////////////
    // Example found here on the forum
    setup_timer_2(T2_DIV_BY_4,255,1); // To get 400Hz, turn on a led
    setup_ccp3(CCP_PWM|CCP_PULSE_STEERING_A);
    output_drive(PIN_E0);

     pwm_value = 1; // 1%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 100; // 10%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 200; // 20%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 300; // 30%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 400; // 40%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 500; // 50%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 600; // 60%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 700; // 70%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 800; // 80%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 900; // 90%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);

     pwm_value = 100; // 100%
     set_pwm3_duty(pwm_value);
     delay_ms(1000);


 
    while(TRUE)
    {
    }
}

_________________
Gradually you will go far with persistence, will and determination!
erpgc82



Joined: 02 May 2020
Posts: 73

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

two CCP3 pins
PostPosted: Thu Apr 15, 2021 7:14 pm     Reply with quote

Hello my friends, gentlemen of PIC programming, I think I am beginning to understand why I am unable to make it work.

I'm using CCP3, pin 25 PIN_E0, and trying to solder the PWM frequency through this pin and I never can.

Looking at the datasheet I see that pin 15 PIN_B5, is also CCP3, that pin is connected to the LCD. And that's probably why the PWM signal is coming out and that is why the LCD has strange characters.

So how do I configure CCP3 to be E0 and not B5?
_________________
Gradually you will go far with persistence, will and determination!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 7:53 pm     Reply with quote

It's done with fuses: CCP3E0 or CCP3B5
erpgc82



Joined: 02 May 2020
Posts: 73

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

resolved
PostPosted: Thu Apr 15, 2021 9:02 pm     Reply with quote

Resolved person, all this because I did not know about this #fuse CCP3E0, this is to never forget ... Thank you all, and sorry for the persistence and lack of knowledge on my part.
_________________
Gradually you will go far with persistence, will and determination!
newguy



Joined: 24 Jun 2004
Posts: 1902

View user's profile Send private message

PostPosted: Thu Apr 15, 2021 10:06 pm     Reply with quote

Glad to hear it.

Moving forward, ensure that you read the processor's data sheet first. Not the whole thing (it's too long!). But you should have read the part of the sheet concerning the peripheral you were having trouble with: the PWM (CCP). That chapter you should have read. You would have quickly seen that the PWM can be routed to 2 pins. Delving deeper, you would have found the fuse selections that govern which pin.

It's okay - we've all done something similar! It's normal! But it's experiences like this that have made us more careful. If I need to use a peripheral I've not used before, I start by reading its chapter in the processor's data sheet. I've found that I bang my head on my desk much less often now that I do. Wink
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 Previous  1, 2
Page 2 of 2

 
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