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

LCD gone wrong after unrelated PIN assigned in .h file

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tfm



Joined: 13 Dec 2014
Posts: 4

View user's profile Send private message

LCD gone wrong after unrelated PIN assigned in .h file
PostPosted: Sat Dec 13, 2014 12:50 am     Reply with quote

Hello,

I am using PIC18F4685, CCS PCWHD Compiler 5.008.

My code involves reading a value from ADC (PIN_A0), setting PWM (PIN_RC2 / CCP1) and sending data to LCD.

LCD is working on individual PIN basis. Everything working great up to the moment I decided to use any pin that happens to be in any port related to LCD, even if the pin to be used in unrelated to LCD operation itself.

LCD pins are defined as below:

#define LCD_ENABLE_PIN PIN_C4
#define LCD_RS_PIN PIN_D2
#define LCD_RW_PIN PIN_D3
#define LCD_DATA4 PIN_C5
#define LCD_DATA5 PIN_C6
#define LCD_DATA6 PIN_C7
#define LCD_DATA7 PIN_D4


For example, just by assigning any pin of PORTC or PORTD as FIXED_IO( x_outputs=PIN_xx ) in .h file, LCD goes wrong. All dots of LCD positions in first line go black, but nothing that should be written gets written. It gets stuck there. Note that the new pins, ie, from PORTC or D will work normally as intended.

Just commenting FIXED_IO( x_outputs=PIN_xx ) line fixes the sw. Also found that moving FIXED_IO( x_outputs=PIN_xx ) from .f file to .c file after #include <lcd.c> also fixes the problem.

My question is why? I haven't seen anything in lcd.c - individual pin part - that could be influenced by the previous FIXED_IO( x_outputs=PIN_xx ) directive.

Full code below. PS: I don't know what happened to indentation!!

.c file

#include <main.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

#define LCD_ENABLE_PIN PIN_C4
#define LCD_RS_PIN PIN_D2
#define LCD_RW_PIN PIN_D3
#define LCD_DATA4 PIN_C5
#define LCD_DATA5 PIN_C6
#define LCD_DATA6 PIN_C7
#define LCD_DATA7 PIN_D4

#include <lcd.c>

#use FIXED_IO( D_outputs=PIN_D0 ) //this line moved to this point makes it work!


volatile int16 int_cnt=0;
volatile int int_cnt_lcd_ok=0;


#INT_TIMER0
void TIMER0_isr(void)
{

if (int_cnt == 1220) //tests for 500ms for LCD
{
int_cnt_lcd_ok=1; //flag 500ms - LCD. Reset in main
}
else
int_cnt++;
}


#INT_RB
void RB_isr(void)
{
int dummy;
dummy = input_b(); //clear mismatch
}


void main()
{
int16 adc=0;
int16 pwm;
float volt_ad=0;

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8|RTCC_8_bit); //16x 819 us overflow ; 8x 409us

setup_adc_ports(AN0_TO_AN1);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);

port_B_pullups(0xFF);

lcd_init();
lcd_putc("\fReady...\n");
delay_ms(400);

enable_interrupts(INT_TIMER0);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);

while(TRUE)
{

adc=read_adc();

if(adc_done()==1)
{
pwm = (int32)adc * 1000/1024;
pwm_set_duty_percent(pwm);
volt_ad = (float)adc * 5/1024;
}


if (int_cnt_lcd_ok==1) //flag
{
lcd_gotoxy(1,1);
printf(LCD_PUTC,"ADC=%Lu Vad=%.2f",adc,volt_ad);
lcd_gotoxy(1,2);
printf(LCD_PUTC,"PWM dc=%.1f%%",(float)pwm/10);
int_cnt_lcd_ok=0;
int_cnt=0;
}
}
}

.h file

#include <18F4685.h>
#device ADC=10

#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES BBSIZ1K //1K words Boot Block size
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(crystal=20000000)

#use FIXED_IO( A_outputs=PIN_A3,PIN_A4,PIN_A5 )

//#use FIXED_IO( C_outputs=PIN_C0 ) //this line will corrupt LCD operation

#use pwm(CCP1,TIMER=2,FREQUENCY=20,DUTY=100)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 1:13 am     Reply with quote

With #fixed_io(), all pins that are not assigned as outputs, are made into inputs.
This line of C code,
Code:
#use FIXED_IO( D_outputs=PIN_D0 )

creates these two lines of ASM code:
Code:

001F0:  MOVLW  FE
001F2:  MOVWF  TRISD

And those two lines are placed by the compiler before every pin i/o
operation for Port D. Your lcd pin assignments have multiple pins
on PortD, and that #fixed_io() statement makes them into inputs.
The problem is, those pins must be outputs (RW, EN) or i/o (D4).
That's why the lcd won't work.

Solution: Do not use #fixed_io(). Let the compiler handle the TRIS.
tfm



Joined: 13 Dec 2014
Posts: 4

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 1:18 am     Reply with quote

PCM programer,

That's right! Thank you!!

In what case(s) are there real advantages using #fixed_io()? I mean, why would this directive be in CCS first place?
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 1:38 am     Reply with quote

There are three port directives like this:

STANDARD_IO
FAST_IO
FIXED_IO

Standard IO is the 'standard'. It makes CCS control all the pins.
Fast IO, makes IO control your responsibility entirely. You set the direction with TRIS, and if you want to change direction you have to change the TRIS.
Fixed IO, is a 'half way house'. CCS still sets the TRIS, but instead of doing it automatically you have manual control of the setting.

Now, reasons:

Two primarily:
1) FAST_IO, is faster. Smile
The TRIS control is only done when you order it. For all normal I/O operations it is not touched, making these faster.

2) Then both FAST_IO, and FIXED_IO allow you to perform port operations and not change the TRIS. Where this is useful is (for example), if you are using 'interrupt on change' on the top four bits of port B. Then in the the interrupt handler, you can perform a 'byte read' on the whole port, without automatically changing the I/O direction of the other four pins.
FIXED_IO still sets the TRIS on every I/O operation, but the setting is the one you specify.
Anything similar to this where you want to perform 'wider' I/O operations on a port and not affect other pins is the primary reason for FIXED_IO.
Reasons against:

Primarily, once you use either of these modes, you have to get the direction right. It is quite complex on some chips to get things like the direction control for peripherals correct, with the same peripheral on different chips needing different settings.

Once you use either of these settings, 'off the shelf' code that relies on using STANDARD_IO will have to be re-written.

Caveats:

CCS gets the I/O control 'right' now 99% of the time. Where it fails, is with things like relocatable peripherals, where you will often have to manually make the settings. However for 95% of programming the CCS STANDARD_IO mode is where to start.

When I started using CCS it probably got things right less often than it got things wrong. My early code tends to always be setting the TRIS manually. Now I probably touch TRIS, and switch modes away from STANDARD_IO, only 'once in a blue moon'.
tfm



Joined: 13 Dec 2014
Posts: 4

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 2:18 am     Reply with quote

Ttelmah, thanks for the reply!

I come from Texas Instruments Code Composer Studio, where everything has to be set "by hand", ie, TRIS for all pins and ports.

I'll give it a try with this CCS structure! Seems like saves time - until one has to debug it, like I just had to do!! Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 3:28 am     Reply with quote

Except the reason was you locked it off.

The CCS LCD code will control the TRIS for you, if it is allowed to. It'll run with STANDARD_IO, and with FAST_IO setting the TRIS as required. The point about 'FIXED_IO', is that you are saying you want the TRIS FIXED at your setting....
tfm



Joined: 13 Dec 2014
Posts: 4

View user's profile Send private message

PostPosted: Sat Dec 13, 2014 10:39 am     Reply with quote

Got it! Thank you! I'll keep it in mind
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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