Author Topic: [SOLVED] ATTINY13A works randomly  (Read 1519 times)

0 Members and 1 Guest are viewing this topic.

Offline m.m.mTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: ir
  • EE MSc Student and Hobbyist
[SOLVED] ATTINY13A works randomly
« on: September 27, 2017, 09:48:14 am »
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:
Code: [Select]
#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!  :phew:
« Last Edit: September 27, 2017, 12:56:14 pm by m.m.m »
25 y/o Electronics Lover
 

Offline andersm

  • Super Contributor
  • ***
  • Posts: 1198
  • Country: fi
Re: ATTINY13A works randomly
« Reply #1 on: September 27, 2017, 10:14:04 am »
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.
Many/most/all? AVRs will by default divide the input clock by 8. The divider can be disabled either by programming the config bits, or at runtime.
 
The following users thanked this post: m.m.m

Offline m.m.mTopic starter

  • Regular Contributor
  • *
  • Posts: 105
  • Country: ir
  • EE MSc Student and Hobbyist
Re: ATTINY13A works randomly
« Reply #2 on: September 27, 2017, 10:18:50 am »
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.
Many/most/all? AVRs will by default divide the input clock by 8. The divider can be disabled either by programming the config bits, or at runtime.
oh it took a lot of time for me to figure out that ratio LOL!  :palm:
thanks for that.
« Last Edit: September 27, 2017, 11:35:10 am by m.m.m »
25 y/o Electronics Lover
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf