[General] Dimmer with PIC16F877A

chrisss

Newbie
Joined
Nov 21, 2024
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
23
Missing CODE/SYNTAX tags ( Added by moderator )
Hello, I’m trying to make a dimmer with the PIC16F877A. I found some code and a circuit that I’m testing to use later.
However, I’m encountering several problems. When I start my simulation, the following message appears:

[PIC16 MEMORY] PC=0x0A69. Attempt to write unimplemented memory location 0x008F with 0x71 ignored. [U1].

I’m not sure what the error could be.
Here’s the code I’m using, along with images of my simulation and circuit. code:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**************************************************************************************
 AC Lamp light dimmer with PIC16F877A microcontroller
 C Code for CCS C compiler
 Crystal oscillator used @ 20MHz
 This is a free software with NO WARRANTY.
 [URL]http://simple-circuit.com/[/URL]
***************************************************************************************/
#define triac_gate    PIN_D2
 
#include <16F877A.h>
#device ADC = 10
#fuses HS,NOWDT,NOPROTECT,NOLVP                     
#use delay(clock = 20MHz)
#use fast_io(d)
#byte OPTION_REG = GETENV("SFR:OPTION_REG")    // get OPTION_REG register address
#bit  INTEDG = OPTION_REG.6                    // INTEDG (external interrupt edge) bit address ( OPTION_REG(6) )
 
int1 ZC = 0;
int16 alpha;
 
#INT_EXT            // external interrupt ISR
void ext_isr(void)
{
  ZC = 1;
 
  // toggle external interrupt edge
  if(INTEDG) INTEDG = 0;
  else       INTEDG = 1;
 
}
 
void main()
{
  output_low(triac_gate);
  output_drive(triac_gate);
 
  setup_adc(ADC_CLOCK_INTERNAL);     // set ADC clock source
  setup_adc_ports(AN0);              // configure AN0 pin as analog
  set_adc_channel(0);                // select channel AN0
 
  clear_interrupt(INT_EXT);          // clear external interrupt flag bit
  enable_interrupts(GLOBAL);         // enable global interrupts
  enable_interrupts(INT_EXT);        // enable external interrupt
 
  delay_ms(500);
 
  while(TRUE)
  {
    alpha = ( 1023 - read_adc() ) * 10;
 
    if (alpha > 9500)
      alpha = 9500;
    else if (alpha < 200)
             alpha = 0;
 
    while(ZC == 0) ;    // wait for zero crossing event
 
    // send firing pulse after alpha
    delay_us(alpha);
    output_high(triac_gate);
    delay_us(200);
    output_low(triac_gate);
 
    ZC = 0;
  }
}
// End of code




 
Last edited by a moderator:

I can't see anything obvious in the code but I'm not familiar with the quirks in CCS compilers.
You need to find out what is actually at "PC=0x0A69" by looking in the listing or map files. That should tell you which instruction is causing the error. I would guess it has something to do with memory allocation, possibly the code was written for a different processor as address 0x008F (in bank 1) isn't mapped to any pin or peripheral.

Brian.
 

As you are writing the ZC variable in the ISR but reading it in the main loop, you need to declare it volatile.
Tying \MCLR\ to +5V (in the top schematic) wil lmake it imposible to program a real device. The bottom schematic is better but check the data sheet for the recommended way to connect \MCLR\.
Susan
 

Hi,

first schematic - I guess this is a simulation.

I don´t think it can work this way, especially the ZCD circuit.

R3 is connected with GND and so is -IN of U2.
Thus the ZC signal needs to come via R2. For it to work .. thue UPPER of your AC soure should be tied to GND, R3 tries this, but has no chance to do so, because you connected the LOWER of the triac with GND. In this case the LOAD pulls the ZC signal more to GND ... and the voltage at R3 is to low to trigger an event.

--> In detail this depends on the function of your load. So please verify R3 voltage with a scope.

--> and/or the next step in the signal line: verify the comparator output.

Also: the comparator datasheet tells in the "Absolute maximum values" that -IN as well as+IN should NEVER go below -0.3V. I don´t think your circuit can guarantee this for +IN.
Although the current is limited by R2 ... Datasheet says: Violating "absolute maximum ratings" may result in damage.
Other datasheets state that you may go beyond voltage limits as long as the current is limited. I miss this information with the TI datasheet SLCS005AF.

Btw: Please post links to your sources (datasheets, web sites) .. so we all have access to the same informations you used .. and the according context

Klaus
 

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…