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

need help for c programming for digital wattmeter.
Goto page Previous  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
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Fri Apr 08, 2011 1:47 pm     Reply with quote

OMG!! Hurmm.. Nevermind. I'll keep trying.. Start from btm.. Sad..:-(
_________________
i'm student that realy need help to learn
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Fri Apr 15, 2011 12:48 pm     Reply with quote

hye everyone.. after all i do my digital wattmeter until last step.. but i still got one error. can someone fix that for me..

Code:

#include <16F870.h>
#device adc=8
#define CLKSPEED 32000000
#define voltage PIN_A1
#define current PIN_A2
#fuses NOWDT, XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=CLKSPEED)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#BYTE PORT_A=0x05
#BYTE PORT_B=0x06
#BYTE PORT_C=0x07

#include <lcd216.c>
#include <math.h>

#use fast_io (a)
#use fast_io (b)
#use fast_io (c)

//#define LCD_RS PIN_B2//no need to define since it has already  written in the lcd216.c//
//#define LCD_EN PIN_B3//no need to define since it has already  written in the lcd216.c//
#define LCD_D4 PIN_B4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7

#define row0 PIN_A1
#define row1 PIN_A2
#define col0 PIN_A3
#define col1 PIN_A4
#define col2 PIN_A5

//keypad layout//
char const keys[2][3]=
{{'0','1','2'},
 {'4','5','6'}
};

#define KBD_DEBOUNCED_FACTOR 33

int16 volt_sensor;
int16 crnt,crnt_rms,volt_rms,pow_calc,pow_fct,phse_diff;
int1 lc=1;

void measure(void);
void keypad(void);


//====================================

short int ALL_ROWS(void)
{
if(input(row0)&input(row1))
return (0);
else
return (1);

}

char kbd_getc()
{
 static byte kbd_call_count;
 static short int kbd_down;
 static char last_key;
 static byte col;
 
 byte kchar;
 byte row;
 
 kchar='\0';
 
 if(++kbd_call_count>KBD_DEBOUNCED_FACTOR)
 {
  switch (col)
   {
    case 0:
    output_low(col0);
    output_high(col1);
    output_high(col2);
   
   
    case 1:
    output_high(col0);
    output_low(col1);
    output_high(col2);
 
   
    case 2:
    output_high(col0);
    output_high(col1);
    output_low(col2);
   
   
   }
  if(kbd_down)
   {
    if(!ALL_ROWS())
      {
         kbd_down=false;
         kchar=last_key;
         last_key='\0';
      }
   }
  else
   {
      if(ALL_ROWS())
         {
            if(!input(row0))
               row=0;
            else if(!input(row1))
               row=1;
         
               
            last_key=KEYS[row][col];
            kbd_down=true;
         }
      else
         {
            ++col;
            if(col==3)
            col=0;
         }
   }
  kbd_call_count=0;
 }
 return(kchar);
}

void main(void)
{

enable_interrupts(int_timer0);
enable_interrupts(global);

set_tris_A(0b11111111);
set_tris_B(0b10000001);
set_tris_C(0b10000000);

PORT_A=0;
PORT_B=0;
PORT_C=0;

delay_ms(CLKSPEED);
setup_adc_ports(all_analog);
setup_adc( ADC_CLOCK_INTERNAL);
set_adc_channel( 0 );

lcd_init();
LCD_PutCmd(CLEAR_DISP);
LCD_SetPosition(first_LINE+0);
printf(LCD_PutChar,"Digital");
LCD_SetPosition (second_LINE+0);
printf(LCD_PutChar,"Wattmeter");
delay_ms(CLKSPEED);
lc=1;

while(true)
{
 measure();
}
}

void measure (void)
{
set_adc_channel(1);
delay_us(25);
volt_sensor=read_adc();

volt_rms=(volt_sensor)/(1.4142);

crnt=(volt_sensor)/(50);

crnt_rms=(crnt)/(1.4142);

pow_calc=(volt_rms)*(crnt_rms);

pow_fct=(volt_sensor*crnt)/(volt_rms)*(crnt_rms);

phse_diff= atan((0.1592)/(50));

keypad();
}

void keypad ()
{
char k;
main();
printf("\r\Starting...");
while (TRUE)
   {
    k=kbd_getc();
    if(k!=0)
    {
      if(k==0)
      printf("%c",volt_rms);
      else if(k==1)
      printf("%c",crnt_rms);
      else if(k==2)
      printf("%c",pow_calc);
      else if(k==4)
      printf("%c",pow_fct);
      else if(k==5)
      printf("%c",phse_diff);
    }
   }
}




the error is "recursion no permitted [MAIN].

_________________
i'm student that realy need help to learn
temtronic



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

View user's profile Send private message

PostPosted: Fri Apr 15, 2011 1:16 pm     Reply with quote

recursion is when you call a function from within the same function, or try to do a process while inside the same process.

HINT...
while in MAIN() you cannot call MAIN() .

ie
void main() {

dothis();
dothat();
main(); <--- this is not allowed

}
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Fri Apr 15, 2011 1:24 pm     Reply with quote

ok.. thanks.. now my program running well.. but now the problem is when i compile it, the error is "out of rom, the segment or the program is too large MAIN." how can i solve this problem.. im using pic 16f870.
_________________
i'm student that realy need help to learn
temtronic



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

View user's profile Send private message

PostPosted: Fri Apr 15, 2011 3:09 pm     Reply with quote

Again, you have to read the onscreen help info that CCS supplies.
While your program may fit into your PIC, it needs to be 'broken up' into smaller chunks of code to fit into the addressing space requirements of the PIC.
This topic is covered in the help file,however, how it's done is up to you.
You'll have to read the datasheet about size of program memory, look at the listing for code size ,decide where to put what,etc.
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Fri Apr 15, 2011 10:47 pm     Reply with quote

Now I'm using pic16f870 which has 128 byte ROM memory. Can I simply change my pic to pic 16f876 which has bigger ROM memory? its about 328 byte. Since it datasheet is almost the same with 16f870.
_________________
i'm student that realy need help to learn
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Apr 16, 2011 4:02 am     Reply with quote

First you are confusing memory types. You would not get far with 128bytes of ROM. Your chip has 2K _words_ (14bit, not bytes), of ROM. The 128bytes, is the RAM. Yes the 886, has a lot more 8K words.
The need to split up though, still applies. The program memory is split into pages. Think of it just like the pages for an engineering drawing. The problem at the moment, is that you have everything drawn on one sheet, and are running out of space _on that sheet_. The 886, gives you more sheets, but the size of the individual sheets remains the same. You need to work like a person drawing a complex engine, who splits the drawing up, into (say) the head assembly, the crank assembly, and the block, so that each can fit nicely on a single sheet. Having more 'sheets', allows a more complex total assembly to be drawn, _but_ each part must still fit on the sheet of paper being used.
A search on the forum for your error message,, will find a lot of descriptions of how to do this.

Best Wishes
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 16, 2011 5:58 am     Reply with quote

i have read the previous forum..

https://www.ccsinfo.com/forum/viewtopic.php?p=69006

but when i change my main().. i try to modified it, it turns to more error. can u help me.. now this is my code before i modify.

Code:


I have removed my code because i have change it.. u can check my new code on the second page.. ty.

_________________
i'm student that realy need help to learn


Last edited by miji88 on Tue May 10, 2011 9:06 am; edited 1 time in total
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 16, 2011 6:00 am     Reply with quote

also, when i apply for PIC16F876. it running well.. no error with one warrning which is "variable never used : rs_232". so it is ok for me to change my pic?
_________________
i'm student that realy need help to learn
temtronic



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

View user's profile Send private message

PostPosted: Sat Apr 16, 2011 6:24 am     Reply with quote

That warning is OK...
When you correctly added the 'errors' option to the use rs232 function, that variable was created by the compiler.Since your program doesn't use it, you get a warning( or reminder).
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 16, 2011 8:50 am     Reply with quote

ok.. thank you sir.. i got it.. thank you very much..
_________________
i'm student that realy need help to learn
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Tue May 10, 2011 9:00 am     Reply with quote

Hi all. I have done my coding. When I compile using ccs c compiler and mplab, its running well. But when I want to burn my hex file into pic using mplab ide.. The mplab state that "target power not detected".. what does it mean?? Is it because of my programming code?? Can someone help me pls...

This is my code.

Code:

#include <16F877.h>
#device adc=8
#define CLKSPEED 32000000
#define voltage PIN_C3
#fuses NOWDT, XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=CLKSPEED)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#BYTE PORT_A=0x05
#BYTE PORT_B=0x06
#BYTE PORT_C=0x07
#BYTE PORT_D=0x08

#include <lcd216.c>
#include <math.h>

#use fast_io (a)
#use fast_io (b)
#use fast_io (c)

//#define LCD_RS PIN_B2//no need to define since it has already  written in the lcd216.c//
//#define LCD_EN PIN_B3//no need to define since it has already  written in the lcd216.c//
#define LCD_D4 PIN_B4
#define LCD_D5 PIN_D5
#define LCD_D6 PIN_D6
#define LCD_D7 PIN_D7

#define row0 PIN_D1
#define row1 PIN_D2
#define row2 PIN_D3
#define col0 PIN_D4
#define col1 PIN_D5
#define col2 PIN_D6
#define col3 PIN_D7

//keypad layout//
char const keys[3][4]=
{{'0','1','2','3'},
 {'4','5','6','7'},
 {'8','9','*','#'}
};

#define KBD_DEBOUNCED_FACTOR 33

int16 volt_sensor;
int16 crnt,crnt_rms,volt_rms,pow_calc,pow_fct,phse_diff;
int1 lc=1;

void kbd_init(void);
void measure(void);
void keypad(void);


//====================================
void kbd_init()
{
port_b_pullups(true);
}

short int ALL_ROWS(void)
{
if(input(row0)&input(row1)&input(row2)&input(row3))
return (0);
else
return (1);

}

char kbd_getc()
{
 static byte kbd_call_count;
 static short int kbd_down;
 static char last_key;
 static byte col;
 
 byte kchar;
 byte row;
 
 kchar='\0';
 
 if(++kbd_call_count>KBD_DEBOUNCED_FACTOR)
 {
  switch (col)
   {
    case 0:
    output_low(col0);
    output_high(col1);
    output_high(col2);
    output_high(col3);
   
    case 1:
    output_high(col0);
    output_low(col1);
    output_high(col2);
    output_high(col3);
 
   
    case 2:
    output_high(col0);
    output_high(col1);
    output_low(col2);
    output_high(col3);
   
    case 3:
    output_high(col0);
    output_high(col1);
    output_high(col2);
    output_low(col3);
   
   
   }
  if(kbd_down)
   {
    if(!ALL_ROWS())
      {
         kbd_down=false;
         kchar=last_key;
         last_key='\0';
      }
   }
  else
   {
      if(ALL_ROWS())
         {
            if(!input(row0))
               row=0;
            else if(!input(row1))
               row=1;
            else if(!input(row2))
               row=2;
         
               
            last_key=KEYS[row][col];
            kbd_down=true;
         }
      else
         {
            ++col;
            if(col==3)
            col=0;
         }
   }
  kbd_call_count=0;
 }
 return(kchar);
}

void main(void)
{

enable_interrupts(int_timer0);
enable_interrupts(global);

set_tris_A(0b11111111);
set_tris_B(0b10000001);
set_tris_C(0b10000000);
set_tris_D(0b10000010);
PORT_A=0;
PORT_B=0;
PORT_C=0;

delay_ms(CLKSPEED);
setup_adc_ports(all_analog);
setup_adc( ADC_CLOCK_INTERNAL);
set_adc_channel( 0 );

lcd_init();
LCD_PutCmd(CLEAR_DISP);
LCD_SetPosition(first_LINE+0);
printf(LCD_PutChar,"Digital");
LCD_SetPosition (second_LINE+0);
printf(LCD_PutChar,"Wattmeter");
delay_ms(CLKSPEED);
lc=1;


while(true)
{
 measure();
}
}

void measure (void)
{
set_adc_channel(0);
delay_us(25);
volt_sensor=read_adc();

volt_rms=(volt_sensor)/(1.4142);

crnt=(volt_sensor)/(50);

crnt_rms=(crnt)/(1.4142);

pow_calc=(volt_rms)*(crnt_rms);

pow_fct=(volt_sensor*crnt)/(volt_rms)*(crnt_rms);

phse_diff= atan((0.1592)/(50));

keypad();
}

void keypad (void)
{
char k;
kbd_init();
lcd_init();
LCD_PutCmd(CLEAR_DISP);
printf("\r\Starting...");
while (TRUE)
   {
    k=kbd_getc();
     LCD_PutCmd(CLEAR_DISP);
    if(k!=0)
    {
      if(k=='1')
      printf("%c",volt_rms);
      else if(k=='2')
      printf("%c",crnt_rms);
      else if(k=='3')
      printf("%c",pow_calc);
      else if(k=='4')
      printf("%c",pow_fct);
      else if(k=='5')
      printf("%c",phse_diff);
    }
   }

}

_________________
i'm student that realy need help to learn
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 12:26 pm     Reply with quote

Quote:
#include <16F877.h>
#device adc=8
#define CLKSPEED 32000000
#define voltage PIN_C3
#fuses NOWDT, XT, PUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=CLKSPEED)


Download the 16F877 data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/30292c.pdf

The graph below shows the highest frequency is 20 MHz. Why do you
think the PIC will run at 32 MHz ? Also, XT mode, if used with a crystal
is only recommended for 4 MHz, maximum.
Quote:

FIGURE 15-1: PIC16F87X-20 VOLTAGE-FREQUENCY GRAPH
(COMMERCIAL AND INDUSTRIAL TEMPERATURE RANGES ONLY)




Quote:
The mplab state that "target power not detected".. what does it mean??

You should look on the Microchip forums or in the MPLAB Help file to
answer that question. Use Google to search for this:
Quote:
site:microchip.com/forums "target power not detected"

Or, put in the "ICDxxxx" error number and your programmer name
into the search.
miji88



Joined: 06 Apr 2011
Posts: 25
Location: Malaysia

View user's profile Send private message Yahoo Messenger

PostPosted: Tue May 10, 2011 1:24 pm     Reply with quote

thanx pcm.. i'll try to change it.. for now.. do i need to change my msdelay to below 4MHz??
_________________
i'm student that realy need help to learn
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 10, 2011 1:30 pm     Reply with quote

What is "msdelay" ? Do you mean #use delay() ? It should always be
set to the same as the PIC oscillator frequency. That's how the compiler
knows what frequency is used by the PIC, and it's necessary for it to be
correct. The compiler uses it to calculate most of the internal delays in
the CCS library code, etc.
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, 3  Next
Page 2 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