Diferencia entre revisiones de «Código Acelerómetro para Codewarrior 10.6»
De Wikitronica
Línea 100: | Línea 100: | ||
} //end while((SCI1D | } //end while((SCI1D | ||
} //end SendMsg | } //end SendMsg | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | ===Conversión de datos=== | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | |||
+ | word hex2bcd(word hex){ | ||
+ | byte dec[4],i; | ||
+ | word bcd; | ||
+ | |||
+ | for (i=0;i<4;i++){ | ||
+ | dec[i] = (byte) (hex%10); | ||
+ | hex = (word) (hex/10); | ||
+ | } | ||
+ | |||
+ | if (hex>0){ | ||
+ | bcd=0xffff; | ||
+ | }else{ | ||
+ | bcd=(word)((word)(dec[3]<<12) + (word)(dec[2]<<8) + (dec[1]<<4) + dec[0]); | ||
+ | } | ||
+ | return bcd; | ||
+ | } //end hex2bcd | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | |||
+ | byte asc2byte(char n_asc) { | ||
+ | byte n; | ||
+ | |||
+ | n = (byte)(n_asc - 0x30); //convert from ascii to int | ||
+ | if(n > 0x09) // if num is $a or larger... | ||
+ | n -= 0x07; // ...sub $7 to correct | ||
+ | if(n > 0x0f) // if lower case was used... | ||
+ | n -= 0x20; // ...sub $20 to correct | ||
+ | if(n > 0x0f) // if non-numeric character... | ||
+ | n = 0x00; // ...default to '0' | ||
+ | return n; | ||
+ | } //end asc2num | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | |||
+ | word asc2word(byte n_asc[2]) { | ||
+ | word n,n2; | ||
+ | |||
+ | // assumes n_asc[0] is MSB, n_asc[1] is LSB | ||
+ | n = (word)(n_asc[0] - 0x30); //convert from ascii to int | ||
+ | if(n > 0x09) // if num is $a or larger... | ||
+ | n -= 0x07; // ...sub $7 to correct | ||
+ | if(n > 0x0f) // if lower case was used... | ||
+ | n -= 0x20; // ...sub $20 to correct | ||
+ | if(n > 0x0f) // if non-numeric character... | ||
+ | n = 0x00; // ...default to '0' | ||
+ | n = (word)(n<<8); // shift into high byte | ||
+ | n2 = (word)(n_asc[1] - 0x30); //convert from ascii to int | ||
+ | if(n2 > 0x09) // if num is $a or larger... | ||
+ | n2 -= 0x07; // ...sub $7 to correct | ||
+ | if(n2 > 0x0f) // if lower case was used... | ||
+ | n2 -= 0x20; // ...sub $20 to correct | ||
+ | if(n2 > 0x0f) // if non-numeric character... | ||
+ | n2 = 0x00; // ...default to '0' | ||
+ | n += n2; // | ||
+ | return n; | ||
+ | } //end asc2word | ||
+ | |||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | char * byte2asc(byte num, byte base) { | ||
+ | byte n; | ||
+ | |||
+ | if (base){ | ||
+ | n=(byte)(hex2bcd(num)); | ||
+ | }else{ | ||
+ | n=num; | ||
+ | } //end if (base) | ||
+ | n_str[0] = (byte)((n>>0x04)+0x30); // convert MSN to ascii | ||
+ | if(n_str[0]>0x39) // if MSN is $a or larger... | ||
+ | n_str[0]+=0x07; // ...add $7 to correct | ||
+ | n_str[1] = (byte)((n&0x0f)+0x30); // convert LSN to ascii | ||
+ | if(n_str[1]>0x39) // if LSN is $a or larger... | ||
+ | n_str[1]+=0x07; // ...add $7 to correct | ||
+ | n_str[2] = 0x00; // add line feed | ||
+ | return (char *) n_str; | ||
+ | } //end byte2asc | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | <syntaxhighlight lang="c"> | ||
+ | |||
+ | |||
+ | |||
+ | char * word2asc(word num, byte base) { | ||
+ | word n; | ||
+ | |||
+ | if (base){ | ||
+ | n=hex2bcd(num); | ||
+ | }else{ | ||
+ | n=num; | ||
+ | } //end if (base) | ||
+ | |||
+ | n_str[0] = (byte)((n>>12)+0x30); // convert MSN to ascii | ||
+ | if(n_str[0]>0x39) // if MSN is $a or larger... | ||
+ | n_str[0]+=0x07; // ...add $7 to correct | ||
+ | n_str[1] = (byte)(((n>>8)&0x0f)+0x30); // convert 2nd MSN to ascii | ||
+ | if(n_str[1]>0x39) // if LSN is $a or larger... | ||
+ | n_str[1]+=0x07; // ...add $7 to correct | ||
+ | n_str[2] = (byte)(((n>>4)&0x0f)+0x30); // convert 2nd MSN to ascii | ||
+ | if(n_str[2]>0x39) // if LSN is $a or larger... | ||
+ | n_str[2]+=0x07; // ...add $7 to correct | ||
+ | n_str[3] = (byte)((n&0x0f)+0x30); // convert 2nd MSN to ascii | ||
+ | if(n_str[3]>0x39) // if LSN is $a or larger... | ||
+ | n_str[3]+=0x07; // ...add $7 to correct | ||
+ | n_str[4] = 0x00; // add line feed | ||
+ | return (char *) n_str; | ||
+ | |||
+ | } //end word2asc | ||
</syntaxhighlight> | </syntaxhighlight> |
Revisión del 16:08 22 jun 2016
Contenido
Descripción general
Descripción de funciones
Inicialización de periféricos
ICS
void ICS_FEI(void) {
if (NVICSTRM != 0xFF)
ICSTRM = NVICSTRM;
else
ICSTRM = 0xAD;
ICSC1 = ICSC1_FEI;
ICSC2 = ICSC2_FEI;
ICSSC = ICSSC_FEI;
while (ICSC1_CLKS != ICSSC_CLKST) {}
} //end
KBI
void InitKBI(void) {
// Enable KBI1P[3:2] as interrupt
KBI1PE = KBI_SW;
KBI1SC = 0b00000110;
/* ||||
|||+---- KBIMOD = KBI detection mode: 0=edge only
||+----- KBIE = KBI int enable: 1=enabled
|+------ KBACK = KBI int acknowledge: 1=clr IRQF
+------- KBF = KBI flag
*/
}
SCI
void InitSCI(word baud) {
SCI1BD = baud; // set baud
}
ICC
void IIC_configuration (void) {
IIC2F = 0x90;
IIC2C1 = 0xC0;
}
Funciones para la transmisión y recepción de datos
Enviar caracteres
char RecChar(void) {
byte rec_char;
if (SCI1S1_RDRF)
rec_char = SCI1D;
SCI1C2_RE = 1;
while(!SCI1S1_RDRF){ };
rec_char = SCI1D;
SendChar((char) rec_char);
return (char) SCI1D;
}
Recibir caracteres
void SendChar(char s_char) {
SCI1C2 = 0x08; // enable Tx
while(!SCI1S1_TDRE){ }
SCI1D = (byte) s_char; // 2nd half of TDRE clear procedure
} //end SendChar
}
Enviar mensaje
void SendMsg(char msg[]) {
byte i=0;
char nxt_char;
SCI1C2 = 0x08; // enable Tx
nxt_char = msg[i++];
while(nxt_char != 0x00) {
while(!SCI1S1_TDRE){}
SCI1D = (byte) nxt_char; // 2nd half of TDRE clear procedure
nxt_char = msg[i++];
} //end while((SCI1D
} //end SendMsg
Conversión de datos
word hex2bcd(word hex){
byte dec[4],i;
word bcd;
for (i=0;i<4;i++){
dec[i] = (byte) (hex%10);
hex = (word) (hex/10);
}
if (hex>0){
bcd=0xffff;
}else{
bcd=(word)((word)(dec[3]<<12) + (word)(dec[2]<<8) + (dec[1]<<4) + dec[0]);
}
return bcd;
} //end hex2bcd
byte asc2byte(char n_asc) {
byte n;
n = (byte)(n_asc - 0x30); //convert from ascii to int
if(n > 0x09) // if num is $a or larger...
n -= 0x07; // ...sub $7 to correct
if(n > 0x0f) // if lower case was used...
n -= 0x20; // ...sub $20 to correct
if(n > 0x0f) // if non-numeric character...
n = 0x00; // ...default to '0'
return n;
} //end asc2num
word asc2word(byte n_asc[2]) {
word n,n2;
// assumes n_asc[0] is MSB, n_asc[1] is LSB
n = (word)(n_asc[0] - 0x30); //convert from ascii to int
if(n > 0x09) // if num is $a or larger...
n -= 0x07; // ...sub $7 to correct
if(n > 0x0f) // if lower case was used...
n -= 0x20; // ...sub $20 to correct
if(n > 0x0f) // if non-numeric character...
n = 0x00; // ...default to '0'
n = (word)(n<<8); // shift into high byte
n2 = (word)(n_asc[1] - 0x30); //convert from ascii to int
if(n2 > 0x09) // if num is $a or larger...
n2 -= 0x07; // ...sub $7 to correct
if(n2 > 0x0f) // if lower case was used...
n2 -= 0x20; // ...sub $20 to correct
if(n2 > 0x0f) // if non-numeric character...
n2 = 0x00; // ...default to '0'
n += n2; //
return n;
} //end asc2word
char * byte2asc(byte num, byte base) {
byte n;
if (base){
n=(byte)(hex2bcd(num));
}else{
n=num;
} //end if (base)
n_str[0] = (byte)((n>>0x04)+0x30); // convert MSN to ascii
if(n_str[0]>0x39) // if MSN is $a or larger...
n_str[0]+=0x07; // ...add $7 to correct
n_str[1] = (byte)((n&0x0f)+0x30); // convert LSN to ascii
if(n_str[1]>0x39) // if LSN is $a or larger...
n_str[1]+=0x07; // ...add $7 to correct
n_str[2] = 0x00; // add line feed
return (char *) n_str;
} //end byte2asc
char * word2asc(word num, byte base) {
word n;
if (base){
n=hex2bcd(num);
}else{
n=num;
} //end if (base)
n_str[0] = (byte)((n>>12)+0x30); // convert MSN to ascii
if(n_str[0]>0x39) // if MSN is $a or larger...
n_str[0]+=0x07; // ...add $7 to correct
n_str[1] = (byte)(((n>>8)&0x0f)+0x30); // convert 2nd MSN to ascii
if(n_str[1]>0x39) // if LSN is $a or larger...
n_str[1]+=0x07; // ...add $7 to correct
n_str[2] = (byte)(((n>>4)&0x0f)+0x30); // convert 2nd MSN to ascii
if(n_str[2]>0x39) // if LSN is $a or larger...
n_str[2]+=0x07; // ...add $7 to correct
n_str[3] = (byte)((n&0x0f)+0x30); // convert 2nd MSN to ascii
if(n_str[3]>0x39) // if LSN is $a or larger...
n_str[3]+=0x07; // ...add $7 to correct
n_str[4] = 0x00; // add line feed
return (char *) n_str;
} //end word2asc