programming C code

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

programming C code

Postby teochiashen » Sun Apr 22, 2012 8:10 pm

How can I learn C code from the beginning? I am so confuse with the code :(
And how to implement fuzzy logic controller in the C code?
teochiashen
Newbie
 
Posts: 13
Joined: Wed Feb 22, 2012 12:00 am

Re: programming C code

Postby shahrul » Sun Apr 22, 2012 9:28 pm

teochiashen WROTE:How can I learn C code from the beginning? I am so confuse with the code :(
And how to implement fuzzy logic controller in the C code?

Do you have basic C language and do you understand Fuzzy Logic?
Programming Fuzzy Logic is converting Fuzzy Graphical into C. Just google 'Fuzzy in C Language'.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: programming C code

Postby teochiashen » Mon Apr 23, 2012 10:12 am

yeah. I am doing a project regarding with fuzzy logic control, I understand fuzzy logic, I just dont know how to implement fuzzy logic in C programming language.
teochiashen
Newbie
 
Posts: 13
Joined: Wed Feb 22, 2012 12:00 am

Re: programming C code

Postby teochiashen » Mon Apr 23, 2012 10:20 am

I try to write the code for microcontroller so that it will display the temperature and water pump on my LCD display, when I compile it after I wrote it on MikroC is no problem, but when I program into my microcontroller, it shows no display at all. I don't know what's the problem? ANd I try to modify the code refer to the book (PIC Microcontrollers: Programming in C). Below is my programming code. The display should be in two lines like this. [(first line) temp:__.__'C (second line) water pump:___] I think maybe is the problem with the ADC conversion. Please help me.


LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB05_bit;
// End LCD module connections

// Initialize variables
unsigned char ch1, ch2;
unsigned int adc_rd_temp, adc_rd_volume;
char txt1[] = "temp:";
char txt2[] = "water pump:";
const temp = 25;
long tlong_temp, tlong_volume;

// Main routine
void main() {

ADC_Init();

INTCON = 0; // All interrupts disabled
ADCON1 = 1; // Set all port A pins as ADC
TRISA = 0xFF; // All port A pins are configured as inputs
TRISB = 0; // All port B pins are configured as outputs
TRISC = 0xF9; // Port C pins RC1 and RC2 are configured as outputs

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD command (cursor off)
Lcd_Cmd(_LCD_CLEAR); // LCD command (clear LCD)

Lcd_Out(1,1,txt1);
Lcd_Chr(1,8,223); // Print degree character, 'C' for Centigrades
Lcd_Chr(1,9,'C');
Lcd_Out(2,1,txt2);
delay_ms(1000);

while(1) {
adc_rd_temp = ADC_Read(0); // A/D conversion. Pin RA0 is an input
delay_ms(1000);
tlong_temp = (long)adc_rd_temp * 5000; // The input multiply with ADC resolution. Convert the result in milivolts
tlong_temp = tlong_temp / 1023; // 0..1023 -> 0-5000mV
tlong_temp = tlong_temp * 100;
ch1 = tlong_temp / 1000; // Extract volts (thousands of millivolts)
// from result
Lcd_Chr(1,6,48+ch1); // Write result in ASCII format
Lcd_Chr_CP('.');
ch1 = (tlong_temp / 100) % 10; // Extract hundreds of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
ch1 = (tlong_temp / 10) % 10; // Extract tens of millivolts
Lcd_Chr_CP(48+ch1); // Write result in ASCII format
delay_ms(1);

adc_rd_volume = ADC_Read(2); // A/D conversion. Pin RA1 is an input
delay_ms(1000);
tlong_volume = ((long)adc_rd_volume*5)/1023; // The input multiply with ADC resolution.
ch2 = tlong_volume;
teochiashen
Newbie
 
Posts: 13
Joined: Wed Feb 22, 2012 12:00 am

Re: programming C code

Postby Brian Griffin » Mon Apr 23, 2012 6:15 pm

Writing a Fuzzy Logic code on a microcontroller seems to be a difficult task. Sometimes you may need to use a seperate program to compile the Fuzzy Logic routines before you put them into the microcontroller.

If you do not have that particular program, have you tried writing/planning these on a paper yet before you write them on the microcontroller? Have you thought on using floating or fixed points for the membership functions? Have you drawn the diagram of truth values for the control system inside? Like, hot, cold, moderately hot, and such?

Plan these on a Matlab, and if it works, then you can slowly 'port' the thing into the microcontroller.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: programming C code

Postby teochiashen » Mon Apr 23, 2012 8:44 pm

I done the fuzzy logic membership functions and parameters in Matlab, I just don't know how to implement it into the programming C code. I found some sample C code about fuzzy logic on internet, but I don't understand.

/* LINGUISTIC hours TYPE char MIN 0 MAX 240 */
/* { */
char hours ;
/* MEMBER day { 55 , 65 , 175 , 185 } */
/*

1-| ...............
| . .
| . .
| . .
0-| ........ ........
----------------------------------
0 60 120 180 240

*/
char hours_day (char __CRISP)
{
if (__CRISP < 55) return(0);
else
{
if (__CRISP <= 65) return(((__CRISP - 55) * 25) + 2);
else
{
if (__CRISP <= 175) return(255);
else
{
if (__CRISP <= 185)
return((( + 185 - __CRISP) * 25) + 2);
else
return(0);
}
}
}
}

for example -- if (__CRISP <= 65) return(((__CRISP - 55) * 25) + 2);
I don't know what is the "55", "25" and "2" stands for?
teochiashen
Newbie
 
Posts: 13
Joined: Wed Feb 22, 2012 12:00 am

Re: programming C code

Postby Brian Griffin » Tue Apr 24, 2012 6:57 pm

Unfortunately we can't help you in 'porting' the code. What can be written on Matlab, can be easily written on a C. You will have to print out the Matlab codes on a paper, grab some empty paper and translate them back one by one with a pen. It is a daunting task but you have to get used to this one.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: programming C code

Postby teochiashen » Thu Apr 26, 2012 9:31 am

How can I check whether the LCD is fine or not? When I supply the power to the LCD connect to the microcontroller, it shows nothing at all. Is it burnt??
teochiashen
Newbie
 
Posts: 13
Joined: Wed Feb 22, 2012 12:00 am

Re: programming C code

Postby Brian Griffin » Thu Apr 26, 2012 10:06 am

teochiashen WROTE:How can I check whether the LCD is fine or not? When I supply the power to the LCD connect to the microcontroller, it shows nothing at all. Is it burnt??


Have you played around with the contrast yet?
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: programming C code

Postby ABSF » Thu Apr 26, 2012 10:25 am

teochiashen WROTE:How can I check whether the LCD is fine or not? When I supply the power to the LCD connect to the microcontroller, it shows nothing at all. Is it burnt??


May be your LCD display has become fuzzy after you put in the fuzzy program.... :lol: Just kidding

this is what my dictionary says. fuzzy=blurred,distorted,ill-defined,out of focus,unclear,vague.....

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 0 guests

cron