Hi, I've just made a attiny13a controlled 3v coincell powered 433MHz transmitter, which just sends first a turn on signal when it's turned on and after some cycles of that it sends a turn off signal (turned on by a single switch button).
The first issue was that when I tried to do a 1ms delay via
_delay_ms function the delay time was wrong, which was not a big deal, I just found the ratio of that error and applied it in the
#define F_CPU line, the chip itself was meant to be clocked by the internal calibrated oscillator rated at 9.6MHz, and I defined the cpu freq at
1.12MHz instead and the delays now work just fine.
The next issue is that it sometimes sends the same wrong code to the transmitter module.
The right code:The wrong code:board layout is shown in the attachments.
code is as in below:
#include <avr/io.h>
#define F_CPU 1120000UL
#include <util/delay.h>
void sendcode (char inputCode[],char leng) {
for (int i=0; i<leng; i++) {
switch (inputCode[i]) {
case 0:
PORTB=0x00;
_delay_us(640);
break;
case 1:
PORTB=0xFF;
_delay_ms(1.4);
break;
case 2:
PORTB=0xFF;
_delay_us(737);
break;
case 3:
PORTB=0xFF;
_delay_us(684);
break;
case 4:
PORTB=0x00;
_delay_us(1307);
break;
}
}
}
int main(void)
{
DDRB=0xff;
char time_for_off=7;
char rep_on=11;
char rep_off=16;
char code_for_turn_on[]={3,4,2,0,1,4,2,4,2};
char turn_on_code_continue[]={4,2,0,1,0,1,0,1,4,3};
char code_for_turn_off[]={3,4,2,0,1,4,2,4,2};
char comm[]={0,1};
while (time_for_off)
{
PORTB=0x00;
sendcode(code_for_turn_on,9);
while(rep_on){
sendcode(comm,2);
rep_on--;
}
sendcode(turn_on_code_continue,10);
PORTB=0x00;
_delay_ms(80);
time_for_off--;
}
while (1)
{
PORTB=0x00;
sendcode(code_for_turn_off,9);
while(rep_off){
sendcode(comm,2);
rep_off--;
}
PORTB=0x00;
_delay_ms(80);
}
}
UPDATE:SOLUTON:
It was all about a stupid capacitor at the transmitter module which was holding a relatively large charge which held the MCU in a error state (maybe triggered the BOD) and after soldering a 1k resistor at Vcc and GND pins of the transmitter the problem is solved!