I am having some problems with a standalone attiny13a for generating morse code messages.
Sometimes the code fails in mysterious ways and other times it works.
Unfortunately it is uncommented.
here is the code:
#define F_CPU 128000
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/io.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define FREQ(x) (1000000/((x)*2))
#define SHT 20
#define LNG (SHT*3)
#define chargap() _delay_ms(LNG)
#define space() _delay_ms(LNG*2)
unsigned char letters[] PROGMEM = {
0x5, 0x18, 0x1A, 0xC, 0x2, 0x12, 0xE,
0x10, 0x4, 0x17, 0xD, 0x14, 0x7, 0x6,
0xF, 0x16, 0x1D, 0xA, 0x8, 0x3, 0x9,
0x11, 0xB, 0x19, 0x1B, 0x1C};
unsigned char numbers[] PROGMEM = {0x3F, 0x2F, 0x27, 0x23, 0x21, 0x20, 0x30, 0x38, 0x3C, 0x3E};
void dash(){
int c;
for(c = 0;c < LNG;c++){
PORTB ^= 0x18;
_delay_ms(1);
}
_delay_ms(SHT);
PORTB = 0;
}
void dot(){
int c;
for(c = 0;c < SHT;c++){
PORTB ^= 0x18;
_delay_ms(1);
}
_delay_ms(SHT);
PORTB = 0;
}
void decodemorse(unsigned char byte){
unsigned char output = 0;
char bit;
for(bit = 7;bit>-1;bit--){
if(byte & (1 << bit)){
if(output) {dash();}
if(!output){output = 1;}
}
else if(output){dot();}
}
}
void txstring(char *s){
int c;
unsigned char code,ch;
for(c = 0;c < strlen(s);c++){
ch = toupper(s[c]);
if(isspace(ch)){space(); continue;}
if(isalpha(ch)){code = pgm_read_byte(&letters[ch-'A']);}
if(isdigit(ch)){code = pgm_read_byte(&numbers[ch-'0']);}
decodemorse(code);
chargap();
}
}
int main(){
DDRB = 0x18;
char ca[15] = {'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
//int t;
while(1){
txstring(ca);
_delay_ms(1000);
}
}
If I use
char s[7]={'T', 'E', 'S', 'T', 0};
txstring(s);
it works but
char s[]="TEST";
txstring(s);
or
txstring("TEST");
does not work.
I cannot figure it out.
I may come and post more info when its not 10min till bed time