MSP430 external port interrupt - external

I am trying get an external interrupt and blink an LED, but I get an error like:
pragma vector = accepts numeric arguments or "unused_interrupts" but not PORT_VECTOR
The code is as follows:
int main(){
// Stop Watch Dog Timer
WDTCTL = WDTPW + WDTHOLD;
P1DIR &= ~BIT4; //configure as an input
P1OUT |= BIT4;
P3DIR |= BIT4;
P3OUT |= BIT4;
P1IES = 0x01;
P1IE = 0x01; //1b = Corresponding port interrupt enabled
P1IFG = 0x01; //PxIFG flag is set with a high-to-low transition
P1REN = 0x01; //Pullup or pulldown enabled
}
#pragma vector = PORT_VECTOR
__interrupt void Port_1 (void){
if( (P1IN & BIT4)==0 ){
// read input as GND
P3OUT |= BIT4;
}
else if( (P1IN & BIT4)==1 ){
P3OUT &= ~BIT4;
}
}

Related

CompressedInt Write?

public static function Read(param1:IDataInput) : int
{
var _loc2_:* = 0;
var _loc3_:int = param1.readUnsignedByte();
var _loc4_:* = (_loc3_ & 64) != 0;
var _loc5_:int = 6;
_loc2_ = _loc3_ & 63;
while(_loc3_ & 128)
{
_loc3_ = param1.readUnsignedByte();
_loc2_ = _loc2_ | (_loc3_ & 127) << _loc5_;
_loc5_ = _loc5_ + 7;
}
if(_loc4_)
{
_loc2_ = int(-_loc2_);
}
return _loc2_;
}
someone can help me with write thing?
about to use in as3 server based but got only read thing
As #Organis correctly said "there's no telling how to compose a reverted algorithm."
The only obvious things are listed below, so you'll have to do a lot of testing to get it right (that's how reverse engineering works, and it might take days or weeks). Good luck.
Assessment:
(1)
public static function Read(param1:IDataInput) : int
Look like it expects a (byte) array with two entries. I suspect you should write a Short (in hex formnat) but it'll be easier to just write two separate decimal values (since a Short is a value that spreads over two bytes).
public static function Write(val1 :int, val2 :int, targetBA :ByteArray) : void
{
targetBA.length = 2;
targetBA.position = 0;
targetBA.writeByte( val1 );
targetBA.writeByte( val2 );
//# target ByteArray can now be "Read" by the other function as "param1"
}
As for the Read side...
Since the function returns a value to update some var, you should use as:
myResult = Read( targetBA );
Where myResult gets the function's returned _loc2_ result.
(2)
var _loc4_:* = ( (_loc3_ & 64) != 0 );
This will give either a 0 or 64. Is 0 if lower than 64, or else is 64 if equal or higher.
This is likely a quick shortcut to setting a Boolean like:
var _loc4_ :Boolean;
if (_loc3_ >= 64) { _loc4_ = true}
else { _loc4_ = false; }
(3)
_loc2_ = _loc3_ & 63;
Where _loc2_ is set as an integer of either 0 or 63.
(4)
while(_loc3_ & 128)
I don't know what this is trying to achieve. (_loc3_ & 128) is either 0 or 128.
This While(0) or While(128) loop will run forever and there isn't any stopping break; at the end.
(5)
_loc2_ = _loc2_ | (_loc3_ & 127) << _loc5_;
This updates _loc2_ with two values. The current loc2 value is combined with a modified loc3 value.
(6)
if(_loc4_)
Likely means if( _loc4_ == true )...

Reading JSON from Serial port missing part of the starting data

When reading a JSON string from the serial port on an ESP8266 it cuts off the beginning of the data.
I have tried reading data from the Serial port and printing each character, however it is cutting off part of the begging of the data.
void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
}
void loop() {
int curSize = 30;
char* buffer = new char[curSize];
std::fill_n(buffer, curSize, 0);
int pos = 0;
Serial.print("Sending: ");
while(Serial.available() == false) delay(500);
while (Serial.available()) {
char c = Serial.read();
Serial.print(c);
if(pos == curSize-1){
char* newBuffer = increaseBuffer(buffer, curSize, curSize + 30);
curSize += 30;
delete[] buffer;
buffer = newBuffer;
}
if(c == '\n'){
buffer[pos] = 0;
pos = 0;
break;
}
buffer[pos++] = c;
}
if(buffer[0] != 0) {
sendBuffer(buffer);
}
delete[] buffer;
}
char* increaseBuffer(char* orig, int oldSize, int newSize){
char* data = new char[newSize];
std::fill_n(data, newSize, 0);
for(int i = 0; i < newSize; i++){
if(i < oldSize) data[i] = orig[i];
else data[i] = '\0';
}
return data;
}
JSON data used (and expected output)
{"type":0,"ver":"0.0.1","T":[28,29,29,29,29,29,29,29,29,29],"H":[59.1608,59.1608,60,59.1608,60,60,60,59.1608,59.1608,59.1608],"DP":[20.36254,20.36254,20.59363,20.36254,20.59363,20.59363,20.59363,20.36254,20.36254],"HI":[30.90588,30.90588,31.0335,30.90588,31.0335,31.0335,31.0335,30.90588,30.90588]}
examples of what is actually output
Example 1: 9,29,29,29,29,29,29,29,29],"H":[59.1608,59.1608,60,59.1608,60,60,60,59.1608,59.1608,59.1608],"DP":[20.36254,20.36254,20.59363,20.36254,20.59363,20.59363,20.59363,20.36254,20.36254],"HI":[30.90588,30.90588,31.0335,30.90588,31.0335,31.0335,31.0335,30.90588,30.90588]}
Example 2: 29,29,29,29,29,29,29,29,29],"H":[59.1608,59.1608,60,59.1608,60,60,60,59.1608,59.1608,59.1608],"DP":[20.36254,20.36254,20.59363,20.36254,20.59363,20.59363,20.59363,20.36254,20.36254],"HI":[30.90588,30.90588,31.0335,30.90588,31.0335,31.0335,31.0335,30.90588,30.90588]}
Try making the delay 1 instead of 500 in the blocking loop that's waiting for data to start coming in. I'm going to guess what happens is that on one iteration of that loop Serial.available() is false and during the delay you start to get data coming in that ends up getting written over by the time your delay ends to check again.
What I'm picturing is the following. If you were to expand out that delay(500) to be delay(1) called 500 times.
while(Serial.available() == false){
delay(1);
delay(1);
// ...
delay(1); // first character comes in
delay(1);
delay(1); // second character comes in
// ...
delay(1); // n character comes in
}
Then after the delay is over you start actually collecting the characters that are coming in.

Inaccurate GPS data using Arduino and GPS/GPRS Module

For a project I´m following this tutorial on how to track the location and output the GPS data using an Arduino with this SIM908 shield. The Arduino correctly sends the GPS data to the database. However, the coordinates are all exactly the same and it seems that they have been rounded off.
For example:
Latitude: 52.216667
Longitude: 5.483333
This isn't because of the PHP script, all it does is put the data it receives in the database. My guess is that it has something to do with the conversion function convert2Degrees.
This is the code we´re running on our Arduino:
int8_t answer;
int onModulePin= 2;
char data[100];
int data_size;
char aux_str[100];
char aux;
int x = 0;
char N_S,W_E;
char url[] = "informatica-corlaer.nl";
char frame[200];
char pin[]="0000";
char apn[]="mmm.nl";
char user_name[]="";
char password[]="";
char latitude[15];
char longitude[15];
char altitude[10];
char date[16];
char time[7];
char satellites[3];
char speedOTG[10];
char course[10];
void setup(){
pinMode(onModulePin, OUTPUT);
Serial.begin(115200);
Serial.println("Starting...");
power_on();
delay(3000);
//sets the PIN code
snprintf(aux_str, sizeof(aux_str), "AT+CPIN=%s", pin);
sendATcommand(aux_str, "OK", 2000);
delay(3000);
// starts the GPS and waits for signal
while ( start_GPS() == 0);
while (sendATcommand("AT+CREG?", "+CREG: 0,1", 2000) == 0);
// sets APN , user name and password
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"APN\",\"%s\"", apn);
sendATcommand(aux_str, "OK", 2000);
snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"USER\",\"%s\"", user_name);
sendATcommand(aux_str, "OK", 2000);
snprintf(aux_str, sizeof(aux_str), "AT+SAPBR=3,1,\"PWD\",\"%s\"", password);
sendATcommand(aux_str, "OK", 2000);
// gets the GPRS bearer
while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
{
delay(5000);
}
}
void loop(){
// gets GPS data
get_GPS();
// sends GPS data to the script
send_HTTP();
delay(5000);
}
void power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin,HIGH);
delay(3000);
digitalWrite(onModulePin,LOW);
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
}
int8_t start_GPS(){
unsigned long previous;
previous = millis();
// starts the GPS
sendATcommand("AT+CGPSPWR=1", "OK", 2000);
sendATcommand("AT+CGPSRST=0", "OK", 2000);
// waits for fix GPS
while(( (sendATcommand("AT+CGPSSTATUS?", "2D Fix", 5000) ||
sendATcommand("AT+CGPSSTATUS?", "3D Fix", 5000)) == 0 ) &&
((millis() - previous) < 90000));
if ((millis() - previous) < 90000)
{
return 1;
}
else
{
return 0;
}
}
int8_t get_GPS(){
int8_t counter, answer;
long previous;
// First get the NMEA string
// Clean the input buffer
while( Serial.available() > 0) Serial.read();
// request Basic string
sendATcommand("AT+CGPSINF=0", "AT+CGPSINF=0\r\n\r\n", 2000);
counter = 0;
answer = 0;
memset(frame, '\0', 100); // Initialize the string
previous = millis();
// this loop waits for the NMEA string
do{
if(Serial.available() != 0){
frame[counter] = Serial.read();
counter++;
// check if the desired answer is in the response of the module
if (strstr(frame, "OK") != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < 2000));
frame[counter-3] = '\0';
// Parses the string
strtok(frame, ",");
strcpy(longitude,strtok(NULL, ",")); // Gets longitude
strcpy(latitude,strtok(NULL, ",")); // Gets latitude
strcpy(altitude,strtok(NULL, ".")); // Gets altitude
strtok(NULL, ",");
strcpy(date,strtok(NULL, ".")); // Gets date
strtok(NULL, ",");
strtok(NULL, ",");
strcpy(satellites,strtok(NULL, ",")); // Gets satellites
strcpy(speedOTG,strtok(NULL, ",")); // Gets speed over ground. Unit is knots.
strcpy(course,strtok(NULL, "\r")); // Gets course
convert2Degrees(latitude);
convert2Degrees(longitude);
return answer;
}
/* convert2Degrees ( input ) - performs the conversion from input
* parameters in DD°MM.mmm’ notation to DD.dddddd° notation.
*
* Sign '+' is set for positive latitudes/longitudes (North, East)
* Sign '-' is set for negative latitudes/longitudes (South, West)
*
*/
int8_t convert2Degrees(char* input){
float deg;
float minutes;
boolean neg = false;
//auxiliar variable
char aux[10];
if (input[0] == '-')
{
neg = true;
strcpy(aux, strtok(input+1, "."));
}
else
{
strcpy(aux, strtok(input, "."));
}
// convert string to integer and add it to final float variable
deg = atof(aux);
strcpy(aux, strtok(NULL, '\0'));
minutes=atof(aux);
minutes/=1000000;
if (deg < 100)
{
minutes += deg;
deg = 0;
}
else
{
minutes += int(deg) % 100;
deg = int(deg) / 100;
}
// add minutes to degrees
deg=deg+minutes/60;
if (neg == true)
{
deg*=-1.0;
}
neg = false;
if( deg < 0 ){
neg = true;
deg*=-1;
}
float numberFloat=deg;
int intPart[10];
int digit;
long newNumber=(long)numberFloat;
int size=0;
while(1){
size=size+1;
digit=newNumber%10;
newNumber=newNumber/10;
intPart[size-1]=digit;
if (newNumber==0){
break;
}
}
int index=0;
if( neg ){
index++;
input[0]='-';
}
for (int i=size-1; i >= 0; i--)
{
input[index]=intPart[i]+'0';
index++;
}
input[index]='.';
index++;
numberFloat=(numberFloat-(int)numberFloat);
for (int i=1; i<=10 ; i++)
{
numberFloat=numberFloat*10;
digit= (long)numberFloat;
numberFloat=numberFloat-digit;
input[index]=char(digit)+48;
index++;
}
input[index]='\0';
}
void send_HTTP(){
uint8_t answer=0;
// Initializes HTTP service
answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
if (answer == 1)
{
// Sets CID parameter
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
if (answer == 1)
{
// Sets url
sprintf(aux_str, "AT+HTTPPARA=\"URL\",\"http://%s/vehicleLocationTransmitter.php?", url);
Serial.print(aux_str);
sprintf(frame, "vehicleID=1&latitude=%s&longitude=%s&altitude=%s&time=%s&satellites=%s",
latitude, longitude, altitude, date, satellites);
Serial.print(frame);
answer = sendATcommand("\"", "OK", 5000);
if (answer == 1)
{
// Starts GET action
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 30000);
if (answer == 1)
{
Serial.println(F("Done!"));
}
else
{
Serial.println(F("Error getting url"));
}
}
else
{
Serial.println(F("Error setting the url"));
}
}
else
{
Serial.println(F("Error setting the CID"));
}
}
else
{
Serial.println(F("Error initializating"));
}
sendATcommand("AT+HTTPTERM", "OK", 5000);
}
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Write a test case for the function in doubt (convert2Degrees())
from the comment: "parameters in DD°MM.mmm’ notation to DD.dddddd°":
If you input: 52°27.123
Then the expected Output should be: 52.45205
Calculation: 52°27.123 = 52 + 27.123/60.0 =
= 52.45205
Further you should post here the value which is input to convert2Degrees()
Your use of strtok is incorrect, and the convert2degrees has major problems.
Here's something derived from NeoGPS that doesn't use the expensive (on AVRs) divide or modulo arithmetic:
//.................................................
// A special divide-by-3 function that avoids division.
// From http://www.hackersdelight.org/divcMore.pdf
static uint32_t divu3( uint32_t n )
{
uint32_t q = (n >> 2) + (n >> 4); // q = n*0.0101 (approx).
q = q + (q >> 4); // q = n*0.01010101.
q = q + (q >> 8);
q = q + (q >> 16);
uint32_t r = n - q*3; // 0 <= r <= 15.
return q + (11*r >> 5); // Returning q + r/3.
}
//------------------------------------------------------------
// Parse the NMEA "DDDMM.mmmm" format for lat and lon.
//
// returns degrees * 10^7
uint32_t parseDDMMmmmm( char *input )
{
uint8_t digits = 0;
uint8_t sixth_digit = 0;
char chr;
// Find the decimal point
while (isdigit( input[digits] ))
digits++;
// All but the last two are degrees.
uint32_t val = 0;
while (digits > 2) {
chr = *input++;
val = val*10 + (chr - '0');
digits--;
}
// convert from degrees to minutes
val *= 60;
// Add in the last 2 minutes digits
uint8_t minutes = 0;
while (digits > 0) {
chr = *input++;
minutes = minutes*10 + (chr - '0');
digits--;
}
val += minutes;
// Decimal point?
chr = *input++;
if (chr == '.') {
chr = *input++;
// Parse up to 6 digits of the fractional minutes.
while ((digits++ < 5) && isdigit( chr )) {
val = val*10 + (chr - '0');
chr = *input++;
}
if ((digits == 6) && isdigit(chr)) {
sixth_digit = chr - '0';
digits++;
}
// Scale the value up to minutes x 1000000.
while (digits < 4) {
val *= 10;
digits++;
}
}
// convert from minutes x 1000000 to degrees x 10000000.
val += divu3( val*2 + 1 ); // same as 10 * (val+30)/60 without truncation
if (digits >= 6) {
if (sixth_digit >= 9)
val += 2;
else if (sixth_digit >= 4)
val += 1;
}
return val;
} // parseDDMMmmmm
...and a floating-point version:
double parseDDMMmmmm_f( char *input )
{
uint8_t digits = 0;
char chr;
// Find the decimal point
while (isdigit( input[digits] ))
digits++;
// All but the last two are degrees.
double val = 0.0;
while (digits > 2) {
chr = *input++;
val = val*10 + (chr - '0');
digits--;
}
// convert from degrees to minutes
val *= 60;
// Add in the last 2 minutes digits
uint8_t minutes = 0;
while (digits > 0) {
chr = *input++;
minutes = minutes*10 + (chr - '0');
digits--;
}
val += minutes;
// Decimal point?
chr = *input++;
if (chr == '.') {
chr = *input++;
// Parse the fractional "mmmm" minutes.
while (isdigit( chr ) && (digits++ <= 4)) {
val = val*10 + (chr - '0');
chr = *input++;
}
// Scale the value up to minutes x 1000000.
while (digits < 4) {
val *= 10;
digits++;
}
}
// convert from minutes x 1000000 to degrees.
val *= 10.0/1000000.0 * 1.0/60.0;
return val;
} // parseDDMMmmmm_f
If you use these functions, you also need to pop the '-' off the front of your AT string, and negate the returned values:
bool south = (latitude[0] == '-');
if (south)
latitude++;
float lat = parseDDMMmmmm_f( latitude );
if (south)
lat = -lat;
convert2Degrees(longitude);
bool west = (longitude[0] == '-');
if (west)
longitude++;
float lon = parseDDMMmmmm_f( longitude );
if (west)
lon = -lon;[/code]

MSP430 and RFM22

i need help!
i have a TI msp-exp430g2 launchpad and a RFM22B and i need them to communicate with each other and i have no idea how to. after along time i came up with the code below to send data to the RFM22. i have an oscilloscope connected to the ANT pin of the RFM22 and i can see only noise and no output!
can anyone tell me what im doing wrong (alot abusively) and maybe someone has an example codes or a project that can help.
#include <msp430.h>
/*
* main.c
*
// MSP430G2xx3
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.2|-> Data Out (UCA0SIMO)
// | |
// LED <-|P1.0 P1.3|-> nSel
// | |
// | P1.4|-> Serial Clock Out (UCA0CLK)
*
*/
//unsigned int address;
//unsigned char data;
void init(void);
void initRFM(void);
void write(int address, char data);
void txRFM(void);
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
int t;
for (t=0;t<150;t++){ // 150 ms now
__delay_cycles(1000); // 1000 usec
}
init();
initRFM();
while(1){
txRFM();
}
return 0;
}
void txRFM(void){
unsigned char i;
write(0x07, 0x01); // To ready mode
__delay_cycles(50);
write(0x08, 0x03); // FIFO reset
write(0x08, 0x00); // Clear FIFO
write(0x34, 64); // preamble = 64nibble
write(0x3E, 17); // packet length = 17bytes
for (i=0; i<17; i++)
{
write(0x7F, 0xAA); // send payload to the FIFO
}
write(0x05, 0x04); // enable packet sent interrupt
write(0x07, 9); // Start TX
}
void write(int address, char data){
P1OUT &= ~BIT3; // start write
address |= 0x80;
UCA0TXBUF = address;
while ( ! ( IFG2 & UCA0TXIFG ) ) ;
UCA0TXBUF = data;
__delay_cycles(20);
P1OUT |= BIT3; // end write
}
void init(void) {
P1DIR |= BIT3; // P1.3 nSEL for writing to RFM22
P1OUT |= BIT3; // no write
P1SEL |= BIT2 + BIT4; // P1.4 clock out, P1.2 data out (UCA0SIMO)
P1SEL2 |= BIT2 + BIT4;
UCA0CTL0 |= UCCKPL + UCMSB + UCMST + UCSYNC; // UCCKPL- inactive high, UCMSB- MSB first, UCMST- Master mode, UCSYNC- sync mode
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 |= 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // Enable USCI0 RX interrupt
}
void initRFM(void){
//write(0x03, 0x00); // Disable all interrupts
write(0x07, 0x01); // Set READY mode
write(0x09, 0x7F); // Cap = 12.5pF
write(0x0A, 0x05); // Clk output is 2MHz
write(0x0B, 0xF4); // GPIO0 is for RX data output
write(0x0C, 0xEF); // GPIO1 is TX/RX data CLK output
write(0x0D, 0x00); // GPIO2 for MCLK output
write(0x0E, 0x00); // GPIO port use default value
write(0x0F, 0x70); // NO ADC used
write(0x10, 0x00); // no ADC used
write(0x12, 0x00); // No temp sensor used
write(0x13, 0x00); // no temp sensor used
write(0x70, 0x20); // No manchester code, no data whiting, data rate < 30Kbps
write(0x1C, 0x1D); // IF filter bandwidth
write(0x1D, 0x40); // AFC Loop
//write(0x1E, 0x0A); // AFC timing
write(0x20, 0xA1); // clock recovery
write(0x21, 0x20); // clock recovery
write(0x22, 0x4E); // clock recovery
write(0x23, 0xA5); // clock recovery
write(0x24, 0x00); // clock recovery timing
write(0x25, 0x0A); // clock recovery timing
//write(0x2A, 0x18);
write(0x2C, 0x00);
write(0x2D, 0x00);
write(0x2E, 0x00);
write(0x6E, 0x27); // TX data rate 1
write(0x6F, 0x52); // TX data rate 0
write(0x30, 0x8C); // Data access control
write(0x32, 0xFF); // Header control
write(0x33, 0x42); // Header 3, 2, 1, 0 used for head length, fixed packet length, synchronize word length 3, 2,
write(0x34, 64); // 64 nibble = 32 byte preamble
write(0x35, 0x20); // 0x35 need to detect 20bit preamble
write(0x36, 0x2D); // synchronize word
write(0x37, 0xD4);
write(0x38, 0x00);
write(0x39, 0x00);
write(0x3A, 's'); // set tx header 3
write(0x3B, 'o'); // set tx header 2
write(0x3C, 'n'); // set tx header 1
write(0x3D, 'g'); // set tx header 0
write(0x3E, 17); // set packet length to 17 bytes
write(0x3F, 's'); // set rx header
write(0x40, 'o');
write(0x41, 'n');
write(0x42, 'g');
write(0x43, 0xFF); // check all bits
write(0x44, 0xFF); // Check all bits
write(0x45, 0xFF); // check all bits
write(0x46, 0xFF); // Check all bits
write(0x56, 0x01);
write(0x6D, 0x07); // Tx power to max
write(0x79, 0x00); // no frequency hopping
write(0x7A, 0x00); // no frequency hopping
write(0x71, 0x22); // GFSK, fd[8]=0, no invert for TX/RX data, FIFO mode, txclk-->gpio
write(0x72, 0x48); // Frequency deviation setting to 45K=72*625
write(0x73, 0x00); // No frequency offset
write(0x74, 0x00); // No frequency offset
write(0x75, 0x53); // frequency set to 434MHz
write(0x76, 0x64); // frequency set to 434MHz
write(0x77, 0x00); // frequency set to 434Mhz
write(0x5A, 0x7F);
write(0x59, 0x40);
write(0x58, 0x80);
write(0x6A, 0x0B);
write(0x68, 0x04);
write(0x1F, 0x03);
}
I haven't worked with your specific radio, but I have worked extensively with TI CCxxxx radios connected with various TI dev kits (launchpad included).
I would begin by ensuring that your hardwareInit() routine sets up the SPI peripheral correctly. When I was developing with TI radios, I would do the following:
// Setup CSn line.
P2DIR |= BIT7;
P2OUT |= BIT7;
P2SEL &= ~BIT7;
P2SEL2 &= ~BIT7;
// Setup the USCIB0 peripheral for SPI operation.
UCB0CTL1 |= UCSWRST;
UCB0CTL0 |= (UCMODE_0 | UCCKPH | UCMSB | UCMST | UCSYNC);
UCB0CTL1 |= UCSSEL_2;
UCB0BR1 = 0;
UCB0BR0 = 2;
// Setup SCLK, MISO, and MOSI lines.
P1SEL |= BIT5 | BIT6 | BIT7;
P1SEL2 |= BIT5 | BIT6 | BIT7;
UCB0CTL1 &= ~UCSWRST;
Then I would test a write() function to ensure that I was writing using the peripheral correctly:
void write(unsigned char address, const unsigned char *buffer, unsigned char count)
{
register volatile unsigned char i; // Buffer iterator
// Change MISO pin to SPI.
P1SEL |= BIT6;
P1SEL2 |= BIT6;
P2DIR &= ~BIT7;
// Look for CHIP_RDYn from radio.
while (P1IN & BIT6);
// Write the address/command byte.
UCB0TXBUF = address;
// Write data byte(s).
for (i = 0; i < count; i++)
{
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = *(buffer+i);
}
// Wait for operation to complete.
while(UCB0STAT & UCBUSY);
P2OUT |= BIT7;
// Change MISO pin to general purpose output (LED use if available).
P1SEL &= ~BIT6;
P1SEL2 &= ~BIT6;
}

Make an application that determines if a sequence of numbers is sorted

Yet another installment of the weekly code-bowling game as the previous incarnation is over a week old and fairly well explored by now. As a refresher:
Code-Bowling is a challenge for
writing the most obscure, unoptimized,
horrific and bastardized code
possible. Basically, the exact
opposite of Code-Golf.
The Challenge:
Create a program that takes a sequence of numbers, and determines if they are in an ascending order.
Example:
$ ./myprogram 1 2 7 10 14
true
$ ./myprogram 7 2 0 1
false
Rules:
There really are none. It can be a console application, it can be a webpage, it can be whatever. It just needs to be a stand-alone program that accepts numbers and returns numbers. The format and methods are 100% up to you.
So have fun, and let's see the bastardized solutions you can come up with!
This uses something I call "Parent Sort". For list greater than size 1, you have to ask Mom or Dad about each pair of numbers. It's interesting because there's a chance that Mom might have you go ask Dad, and there's a bigger chance that Dad will have you go ask Mom. Could run forever assuming infinite stack capabilities.
function askMom($num1, $num2) {
$chance = mt_rand(0,2);
if ($chance>1) {
return askDad($num1, $num2);
} else {
return $num1 <= $num2;
}
}
function askDad($num1, $num2) {
$chance = mt_rand(0,4);
if ($chance>1) {
return askMom($num1, $num2);
} else {
return $num1 <= $num2;
}
}
function parentSort(array $numbers) {
for ($i = 0; $i < count($numbers)-1; $i++) {
$chance = mt_rand(0,1);
if ($chance) {
if (askMom($numbers[$i], $numbers[$i+1])) {
} else {
return false;
}
} else {
if (askDad($numbers[$i], $numbers[$i+1])) {
} else {
return false;
}
}
}
return true;
}
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv){
int a, b;
if (argc > 2){
sscanf(argv[1], "%d", &a);
sscanf(argv[2], "%d", &b);
if (a<=b)
return main(argc-1, argv+1);
printf("false");
exit(0);
};
printf("true");
return 0;
};
This solution has worst-case performance O(n!) and works by generating all possible permutations of the list, and then calculating a number (see the function 'value') that has it's minimum for sequential lists (ascending or descending).
def value(list):
sum = 0
for i in range(len(list)-1):
sum = sum + (list[i]-list[i+1])**2.0
return sum
def drop(lst, i):
if i + 1 >= len(lst):
return lst[:i]
else:
return lst[:i] + lst[i+1:]
class list_permute:
def __init__(self, lst):
self.lst = lst
self.i = -1
self.subiter = None
def __iter__(self):
return self
def next(self):
if len(self.lst) == 1:
if self.i == -1:
self.i = self.i + 1
return self.lst
else:
raise StopIteration()
if self.subiter != None:
try:
return [self.lst[self.i]] + self.subiter.next()
except StopIteration:
self.subiter = None
if self.subiter == None:
self.i = self.i + 1
if self.i >= len(self.lst):
raise StopIteration()
else:
self.subiter = list_permute(drop(self.lst, self.i))
return self.next()
def test(list):
given = value(list)
for i in list_permute(list):
if value(i) < given:
return False
# Test for false positive
if list[0] > list[len(list)-1]:
return False
return True
list = []
print "Feed me your numbers (end with ^C)"
try:
while True:
try:
list.append(int(raw_input()))
except ValueError:
print "NaN"
except (KeyboardInterrupt, EOFError):
pass
print test(list)
Here's a quick one. Interestingly, it should still be pretty efficient, since it only iterates over the terms once. It can only work on numbers between 0 and 255...
array_shift($argv);
$str = str_repeat(chr(0), 256);
foreach ($argv as $key => $element) {
$str[(int) $element] = chr($key + 1);
}
$str = str_replace(chr(0), '', $str);
$hex = unpack('H*', $str);
for ($i = 1; $i < strlen($str); $i++) {
if (substr($hex[1], $i * 2 - 2, 2) != dechex($a)) {
echo "False\n";
die();
}
}
echo "True\n";
It works by inverting the string (1 2 5 4 becomes 1 2 0 4 3, in other words, the number in the sequence becomes the key in the result, and the position in the sequence becomes the value. Then all we need to check is that 1 is in position 1.
And along the same lines (same theory, just set-theory operations):
array_shift($argv);
$vals = array_flip($argv);
ksort($vals);
echo array_values($vals) == range(0, count($vals) - 1) ? "True\n" : "False\n";
This solution isn't unoptimised, but it is obscure, horrific, and bastardised...
/* Either #define macros FIRST, SECOND, THIRD, etc. here, or do so on the
* command line when "compiling" i.e.
* $ gcc -D FIRST=1 -D SECOND=5 -D THIRD=42
*/
#define min(X, Y) ((X) < (Y) ? (X) : (Y))
#define pairsorted(X, Y) (min((X), (Y)) == (X) ? 1 : 0)
#if defined (FIRST) && defined (SECOND) && pairsorted(FIRST, SECOND)
#if defined (THIRD) && pairsorted(SECOND, THIRD)
#if defined (FOURTH) && pairsorted (THIRD, FOURTH)
#if defined (FIFTH) && pairsorted (FOURTH, FIFTH)
#error "Sorted!"
#elif !defined (FIFTH)
#error "Sorted!"
#else /* FIFTH is defined and < FOURTH */
#error "Not sorted!"
#endif /* FIFTH */
#elif !defined (FOURTH)
#error "Sorted!"
#else /* FOURTH is defined and < THIRD */
#error "Not sorted!"
#endif /* FOURTH */
#elif !defined (THIRD)
#error "Sorted!"
#else /* THIRD is defined and < SECOND */
#error "Not sorted!"
#endif /* THIRD */
#elif !defined (SECOND)
#error "Sorted!"
#else /* SECOND is defined and < FIRST */
#error "Not sorted!"
#endif /* SECOND */
#ifndef SECOND
#error "I need at least two values to compare"
#endif
This (ab)uses the C compiler as it's runtime environment, or can be invoked with the following shell script for prettier output (relies on the above being in sortedcpp.c):
#!/bin/bash
ORDINALS=(ZEROTH FIRST SECOND THIRD FOURTH FIFTH)
VALUES=(0 $#)
for i in 1 2 3 4 5; do
if [ $i -le $# ]
then
flags="$flags -D ${ORDINALS[$i]}=${VALUES[$i]}"
fi
done
output=`gcc $flags sortedcpp.c 2>&1`
echo $output | sed -e 's/sortedcpp.c:[0-9]*: error: #error \"\(.*\)\"/\1/'
First time I used dynamic programing to make things worse
It has time and space complexity of O(n²)
#include <stdio.h>
int main (int argc, char **argv)
{
int is_ordered[1000][1000];
int list[1000];
int i,j;
for(i = 1; i < argc; i++)
sscanf(argv[i],"%d", &list[i-1]);
for (i = 0; i < argc -2; i++)
{
if (list[i] < list[i+1])
is_ordered[i][i+1] = 1;
else
is_ordered[i][i+1] = 0;
}
for (i = 2; i < argc -1; i++)
for (j = 0; j < (argc - 1 - i); j++)
{
if (is_ordered[j+1][i+j] && is_ordered[j][i+j-1])
is_ordered[j][j+i] = 1;
else
is_ordered[j][j+i] = 0;
}
if(is_ordered[0][argc-2])
printf("True\n");
else
printf("False\n");
return 0;
}
Yay, Python!
def IsSorted(lst):
return sorted(lst) == lst