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 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
erpgc82



Joined: 02 May 2020
Posts: 73

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

Controlling the Brightness of the Back-Light [Solved]
PostPosted: Mon Apr 12, 2021 11:48 am     Reply with quote

Hello my friends, to the most experienced:

What can you be doing wrong? again i'm having difficulties here. I had no difficulty using the example "EX_TONES.c", but I am having difficulties with decreasing or increasing the brightness of LCD back-light.

In protoboard1 I use a 2N7002 N-channel mosfet, but it never worked.
In the second protoboard I use an NPN BC537.
Both are ideal for the current consumption of the LCD.

I do not want to keep alternating the brightness, I would just like to have a fixed brightness at certain times, and at other times a maximum brightness of the LCD, but I tried several ways with PWM and I couldn't. The LCD characters look strange.

Then I tried an example, found on the internet, with 4Mhz crystal, and even then, it didn't work and the characters are also strange.

Isn't it as simple as in the example to just change the brightness of an LED? Or does the 18F46K22 have something mysterious?

Code:

#include <18f46k22.h>
//#use delay(clock=64Mhz,internal)
#use delay(clock=4Mhz,internal)

#fuses INTRC_IO,PUT,BROWNOUT,NOLVP,NODEBUG,NOMCLR

#use rs232(baud=9600, parity=N, xmit=PIN_D6, rcv=PIN_D7, bits=8, stop=1, ERRORS, stream=read)
#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.c"
#include "tones_18F46K22.c"

#define PWM_LCD       PIN_E0

void test_bright()

void main()
{
   
   int1 test=TRUE;
   // setup_oscillator(OSC_64MHZ,OSC_PLL_ON);
   set_tris_e(0b00000100);
   //setup_adc(ADC_OFF);
   //setup_comparator(NC_NC_NC_NC);
   
   setup_timer_2(T2_DIV_BY_16,255,1);  // changed as an example with 4Mhz crystal, found on the internet.
   setup_ccp3(CCP_PWM);                // Defines that the CCP module will operate in the PWM method, pin E0 CCP3
   
   display_ini();
   set_pwm1_duty(100); // Low backlight brightness
   
   printf(write_display,"\f Test brightness");
   printf(write_display,"\n Low Brightness ");   
   while(TRUE)
   {
       delay_ms(5000);

       if(test)
       {
          set_pwm1_duty(1000); // high backlight brightness. max. 1023
          test=FALSE;     
          test_bright();
       }
    }
}


test_bright();
{
   printf(write_display,"\f Test brightness");
   printf(write_display,"\n High Brightness");
   delay_ms(5000);
   teste=TRUE;
   return;
}

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



Joined: 05 Apr 2011
Posts: 286

View user's profile Send private message

PostPosted: Mon Apr 12, 2021 2:06 pm     Reply with quote

You are using setup_ccp3(CCP_PWM) but then set_pwm1_duty(100);
I think those should either both be '1' or both be '3'.
Also, you will need to use 100L to force 10bit mode or it will assume you want to use 8bit, as 100 is lower than 256.
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Tue Apr 13, 2021 11:15 am     Reply with quote

gaugeguy wrote:
You are using setup_ccp3(CCP_PWM) but then set_pwm1_duty(100);
I think those should either both be '1' or both be '3'.
Also, you will need to use 100L to force 10bit mode or it will assume you want to use 8bit, as 100 is lower than 256.



Hello friend gaugeguy. Today I spent hours and hours trying to make PWM CCP3 work and I was not successful.

Corrected the typos you mentioned. And yet it never leaves the same problem as always. Pwm to change the brightness of an LED does not work and the characters on the LCD are "strange". If I disable everything related to Timer2 / PWM, the characters on the LCD will work normally again. I'm sure this is not hardware, I still believe it is configuration.

From what I learned in the books that teach you how to program C with CCS, PWM is just a configuration, but from all the examples I saw, I just teach you to take an ADC pin and put your digital result as a parameter for the set_pwmx_duty (value) function; or do a "for" to increment a "value" variable to increase or decrease the brightness of an LED.

What I wanted was just at a certain point, the LCD LED would be 50% lit, of course, and at a certain moment 100% lit, very bright.

And from what I've been doing, it's not like that, because I can't make something work that seemed easy.

How the LCD characters look after using the PWM functions, as if it were an interference.


But the LCD is in the portB.
Code:

#define rs PIN_D4 // defines that bit 4 of port C will be called RS: 1 (data) 0 (instruction)
#define en PIN_D5 // defines that bit 5 of port C will be called EN: Down from 5V to 0V, enable writing
#define data output_b // define the entire portB as where the data will be sent


Interesting topic, but it also didn't work
http://www.ccsinfo.com/forum/viewtopic.php?t=56704&highlight=pwm+led

Code:

#include <18f46k22.h>
#use delay(clock=16Mhz,internal)

#fuses INTC_IO, PUT, BROWNOUT, NOLVP, NODEBUG, NOMCLR

#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 standard_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);
    enable_interrupts(INT_RDA2);

    setup_adc_ports(NO_ANALOGS);
    setup_adc(ADC_OFF);
    setup_comparator(NC_NC_NC_NC);
    setup_vref(FALSE);
 
   
    // setup pwm ///////////////////
    // Example found here on the forum
    setup_timer_2(T2_DIV_BY_4,255,1);
    //setup_ccp3(CCP_PWM/*|CCP_PULSE_STEERING_A*/);
    setup_ccp3(CCP_PWM);
    pwm_value = 0;
    //MIN: 1023 - 0%
    //MID: 512  - 50%
    //MAX: 0    - 100%
    set_pwm3_duty(pwm_value);
 
    while(TRUE)
    {
    }
}


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


Last edited by erpgc82 on Thu Apr 15, 2021 9:34 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Tue Apr 13, 2021 11:34 pm     Reply with quote

Connection, connection, connection....

How is the PWM output actually wired to the driver transistor?. How is the
driver actually connected to the LED backlight on the LCD?. How is the
backlight powered and smoothed?. Post a circuit with components and
the pins used.
temtronic



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

View user's profile Send private message

PostPosted: Wed Apr 14, 2021 4:42 am     Reply with quote

hmmm...
saw this in the code...

//MIN: 1023 - 0%
//MID: 512 - 50%
//MAX: 0 - 100%

This just seems 'backwards' to me....

I'd expect if you sent o to the PWM, it'd output nothing, and the 'backlight driver' would not turn the BL LEDs on..
Also driving an LED at 50% doesn't mean we 'see' 50% brightness. There's some special curves for voltage vs lumens, at least with incandescent stage lights, something I kinda remember, from 5 decades ago, prePIC.

As Mr. T says, need to see the 'BL driver' circuit. You should be able to replace the PIC PWM pin with a 5K pot, adjust for 'off', 'dim' and 'on'. Measure the wiper voltage then calculate the PWM value needed for those settings. 2 are easy...... Smile
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Wed Apr 14, 2021 7:11 am     Reply with quote

temtronic wrote:
hmmm...
saw this in the code...

//MIN: 1023 - 0%
//MID: 512 - 50%
//MAX: 0 - 100%

This just seems 'backwards' to me....

I'd expect if you sent o to the PWM, it'd output nothing, and the 'backlight driver' would not turn the BL LEDs on..
Also driving an LED at 50% doesn't mean we 'see' 50% brightness. There's some special curves for voltage vs lumens, at least with incandescent stage lights, something I kinda remember, from 5 decades ago, prePIC.

As Mr. T says, need to see the 'BL driver' circuit. You should be able to replace the PIC PWM pin with a 5K pot, adjust for 'off', 'dim' and 'on'. Measure the wiper voltage then calculate the PWM value needed for those settings. 2 are easy...... Smile


Hello sir temtronic, these informed descriptions, it is from a link that I took in the forum, I made sure to also place the original author of the post.

I will post the circuit as our friend Ttelmah requested.
I really didn't think it would be so "strangely difficult" this way, as it becomes very easy to use PWM in CCS.

Yes, I know that 50% does not mean that the led will emit 50% brightness, and I know the curve.
_________________
Gradually you will go far with persistence, will and determination!
temtronic



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

View user's profile Send private message

PostPosted: Wed Apr 14, 2021 7:16 am     Reply with quote

from the original post...
//
// Set Brightness - set values from 0 - 1024
// MIN: 1023 - 0%
// MID: 512 - 50%
// MAX: 0 - 100%
// Its Inverted


note the /// Its inverted line you lost during 'cut and paste'....

I don't know how the PWM signal drives his 'peripheral', but 0% PWM is zero 100% of the time, 100% PWM is high, 100% of the time. it could be his 'driver ckt' is negative logic, maybe a PNP xtr, ???
that's why a schematic is always necessary.
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Wed Apr 14, 2021 7:36 am     Reply with quote

I set up 2 PCBs. One is with the MOSFET 2N7002 channel N, and the other PCB is with the NPN BC337, in both PCBs, the same thing happens.

It only works if I leave the E0 pin constantly at a high level. I still think it's PWM configuration, that I'm "getting it" or getting it wrong!




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)  // attempt to make CCP2, pin E0 work.

#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);
    enable_interrupts(INT_RDA2);

    setup_adc_ports(NO_ANALOGS);
    setup_adc(ADC_OFF);
    setup_comparator(NC_NC_NC_NC);
    setup_vref(FALSE);
 
    set_tris_e(0b00001100);
    output_e(0b00000001); // If you leave this E0 pin as an output, use fast_io, and set it to a high level,
                          // the LCD will light up.
   
   
    // setup pwm ///////////////////
    // Example found here on the forum
    setup_timer_2(T2_DIV_BY_16,624,1); // To get 400Hz, turn on a led
    setup_ccp3(CCP_PWM|CCP_PULSE_STEERING_B);  /* Configuring the CCP3 in this way,
                                                * the LCD returned to work correctly,
                                                * stopped displaying strange characters.*/   

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

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

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

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

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

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

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

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

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

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

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


 
    while(TRUE)
    {
    }
}


Correct me if I did the wrong calculation to get 400Hz on Pin CCP3 PIN_E0 of PIC18F46K22


If I configure CCP3 as:
CCP_PULSE_STEERING_B
CCP_PULSE_STEERING_C
CCP_PULSE_STEERING_D
CCP_PULSE_STEERING_SYNC

The LCD works correctly, displaying the characters.
IF placing CCP_PULSE_STEERING_A, or omitting, leaving only setup_ccp3 (CCP_PWM) does not work.
_________________
Gradually you will go far with persistence, will and determination!


Last edited by erpgc82 on Thu Apr 15, 2021 9:35 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 14, 2021 7:56 am     Reply with quote

erpgc82 wrote:

// setup pwm ///////////////////
// Example found here on the forum

setup_timer_2(T2_DIV_BY_16,624,1); // To get 400Hz, turn on a led

The CCS manual says:
Quote:

setup_timer2( )
Syntax:
setup_timer_2 (mode, period, postscale);
Parameters:
mode
T2_DISABLED
T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16
period - is a int 0-255 that determines when the clock value is reset
postscale - is a number 1-16 that determines how many timer overflows before an interrupt.

You can't legally put 624 into that function.

CCS manual:
https://www.ccsinfo.com/downloads/ccs_c_manual.pdf
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Wed Apr 14, 2021 10:11 am     Reply with quote

OK thanks! I will learn a lot from the manual, it will be an excellent textbook.

I think I still need to study more, about PWM. I couldn't even see the brightness of the PWM working. I still think it's software configuration.

In the manual, I did not find any reference to the CCS instruction / command, CCP_PULSE_STEERING_B ... I am still in doubt about what it does, and because from its use, it solved the problem of strange characters on the LCD.

I give up for now, I tried in various ways and possibilities, for almost 3 days. In a few days I return to testing PWM.
//___________________________________________________________


A question:
Is there another form of frequency on a PIC pin, other than PWM? as is the example ex_tones.c, where I can swing a pin and get different tones through different frequencies.
_________________
Gradually you will go far with persistence, will and determination!
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Apr 14, 2021 11:19 am     Reply with quote

Key here is the chip data sheet.
The pwm output can be routed to the A, B, C or D output pin.
The default PWM output pin must be one of your LCD pins. You haven't
told us the connections used for this.

Repeat again post your circuit.
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Wed Apr 14, 2021 11:43 am     Reply with quote

Ttelmah wrote:
Key here is the chip data sheet.
The pwm output can be routed to the A, B, C or D output pin.
The default PWM output pin must be one of your LCD pins. You haven't
told us the connections used for this.

Repeat again post your circuit.


Hi Ttelmah, the drawing was posted, follow again

[img]https://i.ibb.co/4PD4DCg/PWM-LCD.jpg[/ img]
Know where the PWM of the lcd led is connected ???
This point, where it is I/O is connected to pin PIN_E0 (CCP3) of 18F46K22

I'm using pin PIN_E0 (CCP3) is where the BC337 is connected to one PCB and the other PCB is 2N7002.

I didn't understand when you say:
"The default PWM output pin must be one of your LCD pins"
????
Code:

#define rs PIN_D4 // defines that bit 4 of port C will be called RS: 1 (data) 0 (instruction)
#define en PIN_D5 // defines that bit 5 of port C will be called EN: Down from 5V to 0V, enable writing
#define data output_b // define the entire portB as where the data will be sent


The pin that emits the PWM signal is not the (CCP3) PIN_E0 ????
_________________
Gradually you will go far with persistence, will and determination!


Last edited by erpgc82 on Thu Apr 15, 2021 9:36 am; edited 2 times in total
temtronic



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

View user's profile Send private message

PostPosted: Wed Apr 14, 2021 4:18 pm     Reply with quote

One possible problem...
In the program you posted at 7:36am(few entries up..) , you've enabled the 2 Interrupts for the 2 UARTS BUT NOT shown any ISR ( handlers..code to do something)

Any 'garbage' coming into either UART will cause the PIC to do 'random' things.....
erpgc82



Joined: 02 May 2020
Posts: 73

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

PostPosted: Wed Apr 14, 2021 5:36 pm     Reply with quote

temtronic wrote:
One possible problem...
In the program you posted at 7:36am(few entries up..) , you've enabled the 2 Interrupts for the 2 UARTS BUT NOT shown any ISR ( handlers..code to do something)

Any 'garbage' coming into either UART will cause the PIC to do 'random' things.....


Hello friend temtronic, the RDA interrupt is configured and working, I learned here on the forum.

The RDA2 interrupt was only enabled, but it didn't exist yet, so I commented the line. It still didn't work.

Tomorrow I will do a test with a direct LED on the PIN_E0 pin (CCP3).
_________________
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

PostPosted: Wed Apr 14, 2021 8:28 pm     Reply with quote

I really can't seem to make it work. I removed the mosfet and turned on a high-brightness blue led.

If I set the PIN_E0 to a high level, I can turn the LED on normally, ok, I have no problems with hardware.

It turns out that I have tried all the numerous possibilities, and the 18f46k22 PWM does not work. Even if I miscalculated, at least I could have come close. But it seems to me that it is a configuration that I do not know how to proceed.
Confused Crying or Very sad

and ok, I will not give up!
Very Happy
_________________
Gradually you will go far with persistence, will and determination!
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  Next
Page 1 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