Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

counter circuit doesn't work (pic 18f4550) please help me!

Status
Not open for further replies.

trunghanh810346

Newbie level 4
Joined
Dec 12, 2017
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
45
I don't know where the error is? I'm a newbie about this and and i just have 1 day later to complete this project, please help me
sorry about my english ability, thank you everybody.
This video shows it work:
https://www.youtube.com/watch?v=scD4xu0PQT4&t=2s
This is my circuit
hinh mach.PNG
This is my code
Code:
#include <stdio.h>
#include <pic18f4550.h> // dinh kem thu vien dinh nghia cua PIC18f4550
#include <xc.h>
//turn off watchdog timer, ext crystal without PLL, activate MLCR pin 
#pragma config WDT = OFF, PLLDIV = 2, CPUDIV = OSC1_PLL2, FOSC = HS, MCLRE = ON , LVP = OFF
//define crystal frequency for dalay function
#define _XTAL_FREQ 8000000 
short int so_dem; // khai bao bien gia tri so dem duoc 
float ty_le_cb=0; // ty le cam bien dan 
int muc=0; // muc xac dinh cua cam bien
int co_vat=0; // bao co vat can hay khong
short int cham=0xff; // dau cham hien thi tren LED 7 doan bao có vat hay khong 
//                       0    1    2    3    4    5    6    7    8    9
unsigned char ma7doan[]={0xC0,0xF9,0xA4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned int ADC_Read(unsigned char channel)
{
  if(channel > 7)              //Channel range is 0 ~ 7
    return 0;

  ADCON0 &= 0xC5;              //Clearing channel selection bits
  ADCON0 |= channel<<3;        //Setting channel selection bits
  __delay_ms(2);               //Acquisition time to charge hold capacitor
  GO_nDONE = 1;                //Initializes A/D conversion
  while(GO_nDONE);             //Waiting for conversion to complete
  return ((ADRESH<<8)+ADRESL); //Return result
}

void main(void) 
{
TRISB = 0;   	//PORTB la output
TRISD = 0;   	//PORTD la output
LATB = 0;
LATD = 0; 
/*TRISC = 1;   	//PORTD la output
LATB = 1;*/
so_dem = Read_b_eep (0);  // doc so dem cu duoc luu trong eeprom tai dia chi 0
ADCON1 = 0x0E;     // RA0 is analog, reference voltage is Vdd,Vss 
TRISAbits.TRISA0 = 1;   // AN0 is input 
/*ADCON2bits.ADFM = 1;  //right justified
ADCON2bits.ACQT = 2;  //ACQT = 4*Tad*/
ADCON2bits.ADCS = 11;  //ADCS = Fosc/8, Tad = 1us internal
ADCON0bits.CHS = 0; // select channel 0
ADCON0bits.ADON = 1;    //enable ADC
		
	while(1) // vong lap vo han
   {
	   LATB = (ma7doan[so_dem/10%10]&cham);
	   LATD = ma7doan[so_dem%10]; 
	   __delay_us(25);
	   ty_le_cb=(ADC_Read(0)*0.39); // doc ty lê % tín hieu tu mat thu hông ngoai (0-100); gia tri adc max=255; 0,39=100/255;
      if(ty_le_cb>70) // khi ty le dan cua mat thu lon hon nguong (70) th́ do là muc 1, tuc khong co vat can            
	 {                             
         muc=1; // xac dinh muc 1;
         cham=0xff; // tat LED bao co vat can
	 }                        
      if(ty_le_cb<30) // khi ty le dan cua mat thu nho hon nguong (30) th́ do là muc 0, tuc co vat can 
	 {
         muc=0; // xac dinh muc 0; 
         cham=0x7f; // bat led bao co vat can
         co_vat=1; // bao co vat can 
	 }
      if(muc==1&&co_vat==1) // neu muc = 1 và co vat can // tuc vat can da di qua cam bien
      {                     
         co_vat=0; // bao khong co vat          
         so_dem++; // tang so dem                                     
         if(so_dem>99) // gioi han so dem den 99
         {
            so_dem=0; // dat lai so dem = 0 khi so dem > 99
         }
       
	 Write_b_eep(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0 
      } 
      //---------------------------------------- 
      if(TRISCbits.TRISC2==0) // khi nút reset so dem duoc nhan
      {                   
         while(TRISCbits.TRISC2==0); // tao vong lap chowf nut duoc nha  
         so_dem=0; // dat so dem ve 0;
	 Write_b_eep(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0 
      }                        
      if(TRISCbits.TRISC0==0) // khi nut giam duoc nhan 
      {                      
         while(TRISCbits.TRISC0==0); // tao vong lap chowf nut duoc nha  
         so_dem--; // giam gia tri so dem
         if(so_dem>99) // neu so dem > 99
         {
            so_dem=99; // nap lai so dem = 99
         }
	 Write_b_eep(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0 
      } 
      if(TRISCbits.TRISC1==0) // khi nut tang duoc nhan
      {                        
         while(TRISCbits.TRISC1==0); // tao vong lap chowf nut duoc nha  
         so_dem++; // tang gia tri so dem
         if(so_dem>99) // neu so dem > 99                             
         {
            so_dem=0; // nap lai so dem = 0
         }
	 Write_b_eep(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0 
      }
   }         
}
 

Hopefully the 220V input is a mistake. If it isn't, I wouldn't look further for the fault!

Check you are reading the correct registers to monitor the switches. It looks like you are reading the TRISC registers instead of PORTC.

Brian.
 
I already checked it but it still doesn't work
proteus told "Compiled successfully" but the code didn't work ( I press reset button , increase or decrease button : it didn't run on LED7 , on the LED it just show 55. ) I don't know where the error is ? can you help me ?
This link is about my (code+circuit+simulation proteus file) ( might you download it and check it for it for me ?)

And by the way I wrote it based on the correct code (worked) in CCS (the teacher asked us to use MPLAB XC8), Can you compare and point out my mistakes ?
thank you so much.
Code:
#device ADC = 8 // dat ADC o che do 8 bit 
#FUSES HS // su dung thach anh ngoai lam bo giao dông
#use delay(clock = 4000000) // su dung thach anh 20M
int8 so_dem; // khai bao bien gia tri so dem duoc 
int8 ty_le_cb; // ty le cam bien dan 
int1 muc = 0; // muc xac dinh cua cam bien
int1 co_vat = 0; // bao co vat can hay khong
int8 cham = 0xff; // dau cham hien thi tren LED 7 doan bao có vat hay khong 
const char ma7doan[] = { 0xC0,0xF9,0xA4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90 }; //khai bao ma 7 doan tich cuc muc thap ( ki tu 8 bit )
																			  //                       0    1    2    3    4    5    6    7    8    9
void main() // chuong trinh chinh 
{
   so_dem=read_eeprom(0); // doc so dem cu duoc luu trong eeprom tai dia chi 0           
   setup_adc_ports(AN0); // thiêt lap kenh ADC AN0 lam ngo vao ADC dung de doc tin hieu tu mat thu hong ngoai
   setup_adc(ADC_CLOCK_INTERNAL); // thiet lap xung cap cho ADC là xung noi                                                                
   set_adc_channel(0); // dat kenh  cho ADC là kenh 0 (AN0)                                                           
   while(TRUE) // vong lap vo han
   {                  
      output_b(ma7doan[so_dem/10%10]&cham); // xuât ma LED 7 doan hang chuc ra port B                   
      output_c(ma7doan[so_dem%10]); // xuât ma LED 7 doan hang don vi ra port C     
      delay_us(25); // tao thoi gian tre us                                         
      ty_le_cb=read_adc()*0.39; // doc ty lê % tín hieu tu mat thu hông ngoai (0-100); gia tri adc max=255; 0,39=100/255;   
      if(ty_le_cb>70) // khi ty le dan cua mat thu lon hon nguong (70) th́ do là muc 1, tuc khong co vat can            
      {                             
         muc=1; // xac dinh muc 1;
         cham=0xff; // tat LED bao co vat can
      }                        
      if(ty_le_cb<30) // khi ty le dan cua mat thu nho hon nguong (30) th́ do là muc 0, tuc co vat can 
      {
         muc=0; // xac dinh muc 0; 
         cham=0x7f; // bat led bao co vat can
         co_vat=1; // bao co vat can 
      }
      if(muc==1&&co_vat==1) // neu muc = 1 và co vat can // tuc vat can da di qua cam bien
      {                     
         co_vat=0; // bao khong co vat          
         so_dem++; // tang so dem                                     
         if(so_dem>99) // gioi han so dem den 99
         {
            so_dem=0; // dat lai so dem = 0 khi so dem > 99
         }
         write_eeprom(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0  
      } 
      //---------------------------------------- 
      if(input(pin_d2)==0) // khi nút reset so dem duoc nhan
      {                   
         while(input(pin_d2)==0); // tao vong lap chowf nut duoc nha  
         so_dem=0; // dat so dem ve 0;
         write_eeprom(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0  
      }                        
      if(input(pin_d0)==0) // khi nut giam duoc nhan 
      {                      
         while(input(pin_d0)==0); // tao vong lap chowf nut duoc nha  
         so_dem--; // giam gia tri so dem
         if(so_dem>99) // neu so dem > 99
         {
            so_dem=99; // nap lai so dem = 99
         }
         write_eeprom(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0  
      } 
      if(input(pin_d1)==0)
      {                        
         while(input(pin_d1)==0); // tao vong lap chowf nut duoc nha  
         so_dem++; // tang gia tri so dem
         if(so_dem>99) // neu so dem > 99                             
         {
            so_dem=0; // nap lai so dem = 0
         }
         write_eeprom(0, so_dem); // ghi so dem vao EEPROM tai dia chi 0  
      }
   }         
}
 

I can't check with CCS or Proteus, I do not have the programs and they don't work in Linux anyway.

As far as I can tell, your program is good so if it still doesn't work I would think the problem lies with declaring the variables, fuses and pin names. The '#...' directives are not part of the 'C' language so they are probably different between CCS and XC8. I advise you to look at a program already written in XC8 to see how they can be adapted. For example (this is NOT for an 18F4550 but shows how to do it):
Code:
/ CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSC oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital input, MCLR internally tied to VDD)
#pragma config BOREN = ON       // Brown-out Detect Enable bit (BOD enabled)
#pragma config LVP = OFF        // Low-Voltage Programming Enable bit (RB4/PGM pin has digital I/O function, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EE Memory Code Protection bit (Data memory code protection off)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
is how the fuses are set in XC8.
You will probably have to define the input pins something like this too:
#define pin_d0 PORTC,0
#define pin_d1 PORTC,1

I am not at a computer with XC8 installed at the moment so I can't help to adapt it for you now.

Brian.
 
yes, I copy fuses from a program available already written in XC8
this is fuses of the code (already written in XC8) i have copied:
Code:
#include <xc.h>
//turn off watchdog timer, ext crystal without PLL, activate MLCR pin 
#pragma config WDT = OFF, PLLDIV = 2, CPUDIV = OSC1_PLL2, FOSC = HS, MCLRE = ON
//define crystal frequency for dalay function
#define _XTAL_FREQ 8000000
I checked and did not know what I missing?

- - - Updated - - -

Sorry to bother everyone,
It just shows (55. on led) when I simulate and I can't press these buttons (reset,increase,decrease)
loi.PNG
and when i press reset (RE3 MCLR), proteus send me these messages:
loi1.PNG
 

I must admit I'm a bit confused as to which compiler you are actually trying to use. The first and 3rd code snippets are written for the XC8 compiler and he 2nd for the MikroC compiler (I think) - and they are very different.
I would step back a bit and make sure that the various parts all work separately before you try putting them all together.
For example, get the LED readout working correctly - even if you just display a value that is a constant in your program with an empty 'while' loop, at least you can put in several constant values (and recompile and run after each edit) to make sure that the values are begin displayed correctly.
Then make sure that you are reading the switches correctly. Your main loop will execute very quickly but you are not taking any account of contact bounce on the switches. Therefore you can expect to see a sequence of '0' and '1' alternating very quickly for several mSec after each push or release and your program will try to respond to each one of them. This is almost certainly not what you want.
The logic for the 'decrement' button might be wrong. After you decrement the variable, you check for it to be greater than 99. I think you want to check that it is less than 0. The variable is declared as a signed integer so it will eventually go negative. If it was an unsigned integer, then your test would work because decremented from 0 would go to a very large positive value rather than negative.
I think that you are trying to write values to the EEPROM - presumably the internal one. You seem to always be using the same EEPROM address for each write and are updating this on each button press. Remember that the EEPROM only has a minimum erase/write cycle count of 100,000 (typically 1 million). You run the risk of having write failures after a while if you keep with this approach.
What is the crystal frequency you are using?
Susan
 

I wrote a new one and didn't use EEPROM but it still didn't work ( compiled successful but the LED didn't show anything ) ,plz help me tomorrow is the deadline for this project.
hinh mach moi.PNG
Code:
#include <xc.h>
//turn off watchdog timer, ext crystal without PLL, activate MLCR pin 
#pragma config WDT = OFF, PLLDIV = 2, CPUDIV = OSC1_PLL2, FOSC = HS, MCLRE = ON 
//define crystal frequency for dalay function
#define _XTAL_FREQ 8000000 
 
unsigned char led7doan[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7F,0x6F};
unsigned int read_adc_channel(char channel)
{
   unsigned int temp;
   ADCON0bits.CHS = channel;
   ADCON0bits.GO = 1;    // start ADC sampling
   while(ADCON0bits.GO == 1);
   temp = ADRES;
   return temp;
}
int dv1,c1,i;
int so_dem; // khai bao bien gia tri so dem duoc 
void HIENTHI ()
{
c1 =  led7doan[so_dem/10%10];
dv1 = led7doan[so_dem%10];
for(i=0;i<2;i++)
		{
		     LATD = ~(8 >> i); // chuyen gia tri sang cua led
		     if(LATDbits.LATD0 == 0){LATB = led7doan[c1];}
		     if(LATDbits.LATD1 == 0){LATB = led7doan[dv1];}
		     __delay_ms(4);
		}	 
}
unsigned int adc_value;
void main(void)
{
    ADCON1 = 0x0D;     // RA0is analog, reference voltage is Vdd,Vss 
    TRISAbits.TRISA0 = 1;   // AN0 is input 
    TRISB = 0;  // PORTB la output
    LATB = 0;
    TRISD = 0;   //PORTD la output
    LATD = 0xFF;   
   // configure adc
    ADCON2bits.ADFM = 1;  //right justified
    ADCON2bits.ACQT = 2;  //ACQT = 4*Tad=4us
    ADCON2bits.ADCS = 11;  //ADCS = Fosc/8, Tad = 1us
    ADCON0bits.ADON = 1;    //enable ADC
    ADRES = 0;     // clear result registers
   // Khai bao gia cac bien
    int so_dem; // khai bao bien gia tri so dem duoc 
    int ty_le_cb=0; // ty le cam bien dan 
    int muc=0; // muc xac dinh cua cam bien
    int co_vat=0; // bao co vat can hay khong
   while(1) // vong lap vo han
   {
      adc_value = read_adc_channel(0);
      ty_le_cb=(adc_value*0.39); // doc ty lê % tín hieu tu mat thu hông ngoai (0-100); gia tri adc max=255; 0,39=100/255;
      if(ty_le_cb>70) // khi ty le dan cua mat thu lon hon nguong (70) th́ do là muc 1, tuc khong co vat can            
	 {                             
         muc=1; // xac dinh muc 1;
	 }         
      if(ty_le_cb<30) // khi ty le dan cua mat thu nho hon nguong (30) th́ do là muc 0, tuc co vat can 
	 {
         muc=0; // xac dinh muc 0; 
         co_vat=1; // bao co vat can 
	 }
      if(muc==1&&co_vat==1) // neu muc = 1 và co vat can // tuc vat can da di qua cam bien
      {                     
         co_vat=0; // bao khong co vat          
         so_dem++; // tang so dem                                     
         if(so_dem>99) // gioi han so dem den 99
         {
            so_dem=0; // dat lai so dem = 0 khi so dem > 99
         }
	HIENTHI ();	
      } 
      // nhan cac nut
       if(TRISCbits.TRISC2==0) // khi nút reset so dem duoc nhan
      {                   
         while(TRISCbits.TRISC2==0); // tao vong lap chowf nut duoc nha  
         so_dem=0; // dat so dem ve 0;
	 HIENTHI (); 
      }     
      if(TRISCbits.TRISC0==0) // khi nut giam duoc nhan 
      {                      
         while(TRISCbits.TRISC0==0); // tao vong lap chowf nut duoc nha  
         so_dem--; // giam gia tri so dem
         if(so_dem>99) // neu so dem > 99
         {
            so_dem=99; // nap lai so dem = 99
         }
	 HIENTHI ();	
      } 
      if(TRISCbits.TRISC1==0) // khi nut tang duoc nhan
      {                        
         while(TRISCbits.TRISC1==0); // tao vong lap chowf nut duoc nha  
         so_dem++; // tang gia tri so dem
         if(so_dem>99) // neu so dem > 99                             
         {
            so_dem=0; // nap lai so dem = 0
         }
	 HIENTHI ();	
      }
   }
}
 

I am using mikroC and Proteus, post your full project and then maybe I'll find some time to check it.

also you should use english only in your code, otherwise it might be hard for others to debug
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top