Jump to content
43oh

oPossum

Members
  • Content Count

    925
  • Joined

  • Last visited

  • Days Won

    103

Reputation Activity

  1. Like
    oPossum reacted to Binish in New MSP430FR5994 LaunchPad Development Kit   
    Hi,
     
        TI has a new launchpad MSP430FR5994 LaunchPad Development Kit  based on the MSP430FR5994.
     
    The new controller is in "Preview" status , but looks interesting -- 256KB FRAM + 8KB RAM + a Low-Energy Accelerator for Vector Math Operations ( TI claims 35x Faster Than ARM Cortex-M0+ Core for 256-Point Complex FFT ).
     
    Check the app note Benchmarking the Signal Processing Capabilities of the Low-Energy Accelerator (Rev. A)  for details.
     
        The launchpad itself is interesting as it has an on-board micro SD card and super capacitor. Has any one got hands on experience with this one? I was thinking of getting one for a data-logging application,  256KB FRAM + SD card  combo is very much tempting. 
     
       Would like to hear from you guys , has TI finally going in the right direction,  as far as MSP430 is concerned, with the LEA in the fight against ARM?
  2. Like
    oPossum reacted to chicken in Chrome for portable UI development (serial, USB)   
    Here's a tutorial on programming a graphical UI in Google Chrome to display data received over serial
    http://www.lucadentella.it/en/2016/06/07/chrome-app-e-comunicazione-seriale/
    via Dangerous Prototypes.
     
    I meant looking into this topic for a long time. For serial communication like in this tutorial, but also USB for a portable upgrade application via a custom USB BSL implementation.
     
  3. Like
    oPossum got a reaction from vintilatimea in Interfacing with DHT11 Humidty + Temp sensor   
    This code will display the DHT11 hex data, and humidity and temperature in decimal.
    It uses software UART so it will work on all versions of the Launchpad.
    DHT11 is connect to P1.4



     
    #include <msp430.h> #include <stdlib.h> volatile unsigned txdata = 0; #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer0_A0(void) { if(txdata) { (txdata & 1) ? (CCTL0 &= ~OUTMOD2) : (CCTL0 |= OUTMOD2); txdata >>= 1; CCR0 += 104; } else { CCTL0 &= ~CCIE; } } void putc(const char c) { while(CCTL0 & CCIE); txdata = 0x0200 | (c << 1); CCR0 = TAR + 16; CCTL0 = OUTMOD0 | CCIE; } void print(const char *s) { while(*s) putc(*s++); } void print_hex_digit(unsigned n) { putc("0123456789ABCDEF"[n & 0x0F]); } void print_hex_byte(unsigned n) { print_hex_digit(n >> 4); print_hex_digit(n); } void print_dec2(int n) { // Print 2 decimal digits 0 to 99 const div_t d = div(n, 10); if(d.quot) putc('0' + d.quot); putc('0' + d.rem); } int read_dht(unsigned char *p) { // Note: TimerA must be continuous mode (MC_2) at 1 MHz const unsigned b = BIT4; // I/O bit const unsigned char *end = p + 6; // End of data buffer register unsigned char m = 1; // First byte will have only start bit register unsigned st, et; // Start and end times // p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0; // Clear data buffer // P1OUT &= ~b; // Pull low P1DIR |= b; // Output P1REN &= ~b; // Drive low st = TAR; while((TAR - st) < 18000); // Wait 18 ms P1REN |= b; // Pull low P1OUT |= b; // Pull high P1DIR &= ~b; // Input // st = TAR; // Get start time for timeout while(P1IN & if((TAR - st) > 100) return -1; // Wait while high, return if no response et = TAR; // Get start time for timeout do { // st = et; // Start time of this bit is end time of previous bit while(!(P1IN & ) if((TAR - st) > 100) return -2; // Wait while low, return if stuck low while(P1IN & if((TAR - st) > 200) return -3; // Wait while high, return if stuck high et = TAR; // Get end time if((et - st) > 110) *p |= m; // If time > 110 us, then it is a one bit if(!(m >>= 1)) m = 0x80, ++p; // Shift mask, move to next byte when mask is zero } while(p < end); // Do until array is full // p -= 6; // Point to start of buffer if(p[0] != 1) return -4; // No start bit if(((p[1] + p[2] + p[3] + p[4]) & 0xFF) != p[5]) return -5; // Bad checksum // return 0; // Good read } void main(void) { unsigned char dht[6]; // Data from DHT11 int err; // unsigned n; // // WDTCTL = WDTPW | WDTHOLD; // Disable watchdog reset DCOCTL = 0; // Run at 1 MHz BCSCTL1 = CALBC1_1MHZ; // DCOCTL = CALDCO_1MHZ; // CCTL0 = OUT; // Setup serial tx I/O P1SEL = BIT1; // P1DIR = BIT1; // TACTL = TASSEL_2 | MC_2; // TimerA SMCLK, continuous __enable_interrupt(); // // for(; { // for-ever while(CCTL0 & CCIE); // Wait for any serial tx to finish err = read_dht(dht); // Read DHT11 if(err) { // If error... print("Error -"); // putc('0' - err); // print("\r\n"); // } else { // No error... for(n = 0; n < sizeof(dht); ++n) { // Show hex data print_hex_byte(dht[n]); // putc(' '); // } // print(" "); // print_dec2(dht[1]); // Show humimidy print(" RH "); // print_dec2(dht[3]); // Show temperature print(" C\r\n"); // } // __delay_cycles(1000000); // Wait a while } // }  
     
  4. Like
    oPossum got a reaction from agaelema in How to use temperature calibration data   
    [tipdf]SLAU144[/tipdf] chapter 24 describes the calibration data stored as TLV (tag length value) in info segment A. There are ADC values for 30C and 85C for both 1.5V and 2.5V internal reference voltages. These temperature calibration values can be used to improve the accuracy of the internal temperature sensor. There is no explanation of how to do this, so here is how to do it...
     
    Before using calibration data it would be a good idea to validate the info segment checksum. The checksum is stored in the first word and is the negative of the XOR of all of the following words. This function will return 0 for a valid checksum.

    unsigned verify_info_chk(const unsigned * const begin, const unsigned * const end) { const unsigned *p = begin + 1; // Begin at word after checksum unsigned chk = 0; // Init checksum while(p < end) chk ^= *p++; // XOR all words in segment return chk + *begin; // Add checksum - result will be zero if checksum is valid }
     
    Each chunk of calibration data in the info segment has a unique tag. This function will search for a specified tag and return a pointer to it. It will return NULL if the tag is not found.

    void const *find_tag(const unsigned * const begin, const unsigned * const end, const unsigned tag) { const unsigned *p = begin + 1; // Begin at word after checksum do { // const unsigned d = *p++; // Get a word if((d & 0xFF) == tag) return (void *)p; // Return pointer if tag matches p += (d >> 9); // Point to next tag } while(p < end); // While still within segment return 0; // Not found, return NULL pointer }
     
    A structure would be handy for using the ADC/temperature cal data...

    typedef struct { unsigned adc_gain; // ADC gain unsigned adc_offet; // ADC offset unsigned ref15; // ADC value of 1.5 volt input when using 1.5 volt reference unsigned t3015; // ADC value of 30C when using 1.5 volt reference unsigned t8515; // ADC value of 85C when using 1.5 volt reference unsigned ref25; // ADC value of 2.5 volt input when using 2.5 volt reference unsigned t3025; // ADC value of 30C when using 2.5 volt reference unsigned t8525; // ADC value of 85C when using 2.5 volt reference } TCAL;
     
    Find tag 0x08 and setup a pointer to the struct...

    const TCAL * const cal = (TCAL *)find_tag(info_seg_a, info_seg_a_end, 0x08);
     
    Now the scale and offset values for the temperature conversion formulas can be calculated...

    const long cc_scale = ((85L - 30) << 16) / (cal->t8515 - cal->t3015); const long cc_offset = -(cal->t3015 * cc_scale) + (30L << 16) + (1 << 15); const long ck_offset = cc_offset + (273.15 * (1L << 16)); const long cf_scale = ((185L - 86) << 16) / (cal->t8515 - cal->t3015); const long cf_offset = -(cal->t3015 * cf_scale) + (86L << 16) + (1 << 15);
     
    Those formulas show the derivation, they can be simplified and combined with basic error handling. The scale and offset values will be zero if the checksum is invalid or the calibration data is not found.

    const TCAL * const cal = (TCAL *)(verify_info_chk(info_seg_a, info_seg_a_end) \ ? 0 \ : find_tag(info_seg_a, info_seg_a_end, 0x08)); const long cc_scale = cal ? 3604480L / (cal->t8515 - cal->t3015) : 0; const long cf_scale = cal ? 6488064L / (cal->t8515 - cal->t3015) : 0; const long cc_offset = cal ? 1998848L - (cal->t3015 * cc_scale) : 0; const long cf_offset = cal ? 5668864L - (cal->t3015 * cf_scale) : 0; const long ck_offset = cc_offset + 17901158L;
     
    In some cases you may want to default to the uncalibrated values...

    const TCAL * const cal = (TCAL *)(verify_info_chk(info_seg_a, info_seg_a_end) \ ? 0 \ : find_tag(info_seg_a, info_seg_a_end, 0x08)); const long cc_scale = cal ? 3604480L / (cal->t8515 - cal->t3015) : 27069L; const long cf_scale = cal ? 6488064L / (cal->t8515 - cal->t3015) : 48724L; const long cc_offset = cal ? 1998848L - (cal->t3015 * cc_scale) : -18169625L; const long cf_offset = cal ? 5668864L - (cal->t3015 * cf_scale) : -30634388L; const long ck_offset = cc_offset + 17901158L;
     
     
    Now the actual scaled temperature values can be calculated with the usual scale/offset formulas...

    dcc = ((cc_scale * adc) + cc_offset) >> 16; // C calibrated dkc = ((cc_scale * adc) + ck_offset) >> 16; // K calibrated dfc = ((cf_scale * adc) + cf_offset) >> 16; // F calibrated
     
    Code for Nokia 7110 that will show uncalibrated (left) and calibrated (right) temperature in F, C, and K.
     

     
    main.cpp

    #include #include #include "nokia7110tl.h" using namespace nokia7110; /* P1.0 Reset P1.3 Temp Sensor (Not used) P2.0 Serial Data P2.1 Backlight P2.2 Chip Select P2.3 Data/Command P2.4 Serial Clock P2.5 Button */ // P1 static const unsigned LCD_RESET = BIT0; static const unsigned RXD = BIT2; static const unsigned SWITCH = BIT3; // P2 static const unsigned LCD_DATA = BIT0; static const unsigned LCD_BACKLIGHT = BIT1; static const unsigned LCD_CE = BIT2; static const unsigned LCD_DC = BIT3; static const unsigned LCD_CLK = BIT4; static const unsigned LCD_BTN = BIT5; Nokia7110 lcd; // Print integer from -999 to 9999 using 12 x 16 font void print_int(int i, unsigned x, const unsigned y) { if(i < -999 || i > 9999) return; const unsigned e = x; x += 48; const unsigned neg = i < 0; if(neg) i = -i; div_t d; d.quot = i; do { d = div(d.quot, 10); lcd.pd12(d.rem, x -= 12, y); } while(d.quot); if(neg) lcd.pd12(14, x -= 12, y); while(x > e) lcd.pd12(10, x -= 12, y); } // Print integer from -999 to 9999 using 6 x 8 font void print_int_small(int i, unsigned x, const unsigned y) { if(i < -999 || i > 9999) return; const unsigned e = x; x += 24; const unsigned neg = i < 0; if(neg) i = -i; div_t d; d.quot = i; do { d = div(d.quot, 10); lcd.print(x -= 6, y, '0' + d.rem); } while(d.quot); if(neg) lcd.print(x -= 6, y, '-'); while(x > e) lcd.print(x -= 6, y, ' '); } #pragma vector = ADC10_VECTOR // ADC conversion complete interrupt __interrupt void ADC10_ISR(void) // { // __bic_SR_register_on_exit(LPM0_bits); // Wakeup main code } // unsigned verify_info_chk(const unsigned * const begin, const unsigned * const end) { const unsigned *p = begin + 1; // Begin at word after checksum unsigned chk = 0; // Init checksum while(p < end) chk ^= *p++; // XOR all words in segment return chk + *begin; // Add checksum - result will be zero if checksum is valid } void const *find_tag(const unsigned * const begin, const unsigned * const end, const unsigned tag) { const unsigned *p = begin + 1; // Begin at word after checksum do { // const unsigned d = *p++; // Get a word if((d & 0xFF) == tag) return (void *)p; // Return pointer if tag matches p += (d >> 9); // Point to next tag } while(p < end); // While still within segment return 0; // Not found, return NULL pointer } typedef struct { unsigned adc_gain; // ADC gain unsigned adc_offet; // ADC offset unsigned ref15; // ADC value of 1.5 volt input when using 1.5 volt reference unsigned t3015; // ADC value of 30C when using 1.5 volt reference unsigned t8515; // ADC value of 85C when using 1.5 volt reference unsigned ref25; // ADC value of 2.5 volt input when using 2.5 volt reference unsigned t3025; // ADC value of 30C when using 2.5 volt reference unsigned t8525; // ADC value of 85C when using 2.5 volt reference } TCAL; int main(void) { unsigned adc; int dc, dk, df; // Temperature in degrees C, K, and F int dcc, dkc, dfc; // Calibrated temperatures const unsigned * const info_seg_a = (unsigned *)0x10C0; // Address of info segement A const unsigned * const info_seg_a_end = info_seg_a + 32; // 32 words in each segment WDTCTL = WDTPW | WDTHOLD; P1REN = RXD | SWITCH; P1DIR = LCD_RESET; P1OUT = RXD | SWITCH | LCD_RESET; P2DIR = LCD_DC | LCD_CE | LCD_CLK | LCD_BACKLIGHT | LCD_DATA; P2REN = LCD_BTN; P2OUT = LCD_CLK | LCD_DC | LCD_CE | LCD_BACKLIGHT | LCD_BTN; ADC10CTL0 = 0; // Configure ADC ADC10CTL1 = INCH_10 | ADC10DIV_3; // ADC10CTL0 = SREF_1 | ADC10SHT_3 | REFON | ADC10ON | ADC10IE; ADC10CTL0 |= ADC10IE; // Enable ADC conversion complete interrupt // _EINT(); // Enable interrupts // const TCAL * const cal = (TCAL *)(verify_info_chk(info_seg_a, info_seg_a_end) \ ? 0 \ : find_tag(info_seg_a, info_seg_a_end, 0x08)); const long cc_scale = cal ? 3604480L / (cal->t8515 - cal->t3015) : 0; const long cf_scale = cal ? 6488064L / (cal->t8515 - cal->t3015) : 0; const long cc_offset = cal ? 1998848L - (cal->t3015 * cc_scale) : 0; const long cf_offset = cal ? 5668864L - (cal->t3015 * cf_scale) : 0; const long ck_offset = cc_offset + 17901158L; lcd.reset(); // lcd.init(); // // lcd.clear(); // // for(; { // for-ever ADC10CTL0 |= (ENC | ADC10SC); // Begin ADC conversion __bis_SR_register(LPM0_bits + GIE); // Sleep until conversion complete adc = ADC10MEM; // Read ADC // // Convert to temperature dc = ((27069L * adc) - 18169625L) >> 16; // C dk = ((27069L * adc) - 268467L) >> 16; // K df = ((48724L * adc) - 30634388L) >> 16; // F // dcc = ((cc_scale * adc) + cc_offset) >> 16; // C calibrated dkc = ((cc_scale * adc) + ck_offset) >> 16; // K calibrated dfc = ((cf_scale * adc) + cf_offset) >> 16; // F calibrated // // Display on LCD print_int(df, 0, 0); // Degrees F print_int(dfc, 48, 0); // Degrees F calibrated print_int(dc, 0, 3); // Degrees C print_int(dcc, 48, 3); // Degrees C calibrated print_int(dk, 0, 6); // Degrees K print_int(dkc, 48, 6); // Degrees K calibrated } // return 0; }
     
    nokia7110tl.h

    namespace nokia7110 { unsigned char PNONE; typedef enum { lcd_command = 0, // Array of one or more commands lcd_data = 1, // Array of one or more bytes of data lcd_data_repeat = 2 // One byte of data repeated } lcd_cmd_type; template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> struct Nokia7110 { void write(const unsigned char *cmd, unsigned len, const lcd_cmd_type type = lcd_data); void reset(void); void init(void); void home(void); void pos(unsigned char x, unsigned char y); void clear(unsigned char x = 0); void fill(unsigned x, unsigned y, unsigned w, unsigned h, unsigned char z); void bitmap(const unsigned char *bmp, signed char x, signed char y, unsigned char w, unsigned char h); inline void bitmap(const unsigned char *bmp, signed char x, signed char y) { bitmap(bmp + 2, x, y, bmp[0], bmp[1]); }; void print(char c); inline void print(unsigned char x, unsigned char y, char c) { pos(x, y); print(c); }; void print(const char *s); inline void print(unsigned char x, unsigned char y, const char *s) { pos(x, y); print(s); }; void print(const char *s, unsigned char m); void printv(unsigned char x, unsigned char y, char *s); void pd12(unsigned n, unsigned x, unsigned y); }; template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::write(const unsigned char *cmd, unsigned len, const lcd_cmd_type type) { register unsigned mask; if(&_EP != &PNONE) _EP &= ~_CE; do { mask = 0x0080; do { if(*cmd & mask) { _SP |= _DATA; _SP &= ~_CLK; } else { _SP &= ~(_CLK | _DATA); } _SP |= _CLK; mask >>= 1; } while(!(mask & 1)); if(!type) { if(&_CP == &PNONE) { __delay_cycles(_DC); } else { _CP &= ~_DC; } } if(*cmd & mask) { _SP |= _DATA; _SP &= ~_CLK; } else { _SP &= ~(_CLK | _DATA); } _SP |= _CLK; if(&_CP != &PNONE) _CP |= _DC; if(!(type & 2)) ++cmd; } while(--len); if(&_EP != &PNONE) _EP |= _CE; } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::reset(void) { if(&_RP == &PNONE) { // --- Set initial state of CLK, DC and CE as needed // * = output used for reset if(&_CP == &PNONE) { if(&_EP != &PNONE) { // CLK*, DATA, CE _EP |= _CE; } // else // CLK*, DATA } else { if(&_EP != &PNONE) { // CLK, DATA, DC*, CE if(&_SP == &_EP) { _SP |= (_CLK | _CE); } else { _SP |= _CLK; _EP |= _CE; } } else { // CLK, DATA, DC* _SP |= _CLK; } } // --- Reset pulse on CLK or DC as needed if(&_CP == &PNONE) { // No DC port, use CLK to reset _SP &= ~_CLK; __delay_cycles(_RD); _SP |= _CLK; } else { // Use DC to reset _CP &= ~_DC; __delay_cycles(_RD); _CP |= _DC; } } else { _RP &= ~_RST; if(&_EP != &PNONE) { if(&_SP == &_EP) { _SP |= (_CLK | _CE); } else { _SP |= _CLK; _EP |= _CE; } } else { _SP |= _CLK; } __delay_cycles(_RD); _RP |= _RST; } __delay_cycles(_RD); } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::init(void) { /* static const unsigned char init[] = { 0x20 | 0x01, // Function set - extended instructions enabled //0x80 | 64, // Set vop (contrast) 0 - 127 0x80 | 70, // Higher contrast improves animation 0x04 | 2, // Temperature control 0x10 | 3, // Set bias system 0x20 | 0x00, // Function set - chip active, horizontal addressing, basic instructions 0x08 | 0x04 // Display control - normal mode }; */ static const unsigned char init[] = { 0xA6, //Display: Normal 0xA3, //LCD Bias Settings: 1/7 0xA1, //ADC Selection: Reverse 0xC0, //Common Output: Normal Direction //0xC8, //Common Output: Upside Down 0x22, //Set the V5 output Voltage 0x81, //Set Electronic Volume Register 0x2E, //Power Controller Set // Booster circuit: ON // Voltage regulator circuit: ON // Voltage follower circuit: OFF 0x2F, //Power Controller Set // Voltage follower circuit: ON 0xE3, //Non-OPeration Command 0x40, //Set the start line 0xAF, //LCD On //0xA5, //Display All Points: ON 0xA4, //Display All Points: NORMAL }; write(init, sizeof(init), lcd_command); } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::home(void) { //static const unsigned char home[] = { 0x40, 0x80 }; static const unsigned char home[] = { 0xB0, 0x11, 0x02 }; write(home, sizeof(home), lcd_command); } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::pos(unsigned char x, unsigned char y) { unsigned char c[3]; /*c[0] = 0x80 | x; c[1] = 0x40 | y;*/ x += 18; c[0] = 0xB0 | y; c[1] = 0x10 | (x >> 4); c[2] = x & 0x0F; write(c, sizeof(c), lcd_command); } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::clear(unsigned char x) { #if 0 home(); write(&x, 504, lcd_data_repeat); home(); #else for(unsigned y = 0; y < 9; ++y) { pos(0, y); write(&x, 96, lcd_data_repeat); } #endif } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::fill(unsigned x, unsigned y, unsigned w, unsigned h, unsigned char z) { #if 0 unsigned yy = y + h; unsigned char c[2]; c[0] = 0x80 | x; for(;y < yy; ++y) { c[1] = 0x40 | y; write(c, sizeof(c), lcd_command); write(&z, w, lcd_data_repeat); } #else unsigned yy = y + h; unsigned char c[3]; x += 18; c[1] = 0x10 | (x >> 4); c[2] = (x & 0x0F); for(;y < yy; ++y) { c[0] = 0xB0 | y; write(c, sizeof(c), lcd_command); write(&z, w, lcd_data_repeat); } #endif } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::bitmap(const unsigned char *bmp, signed char x, signed char y, unsigned char w, unsigned char h) { unsigned char c[3]; unsigned char ww; if(x < 0) { ww = w + x; bmp -= x; x = 0; } else if(x + w >= 96) { ww = (96 - x); } else { ww = w; } /*c[0] = 0x80 | x; c[1] = 0x40 | y;*/ x += 18; c[0] = 0xB0 | y; c[1] = 0x10 | (x >> 4); c[2] = x & 0x0F; while(h--) { write(c, sizeof(c), lcd_command); write(bmp, ww); bmp += w; //++c[1]; ++c[0]; } } static const unsigned char font[96][5] = { 0x00, 0x00, 0x00, 0x00, 0x00, // 0x00, 0x00, 0x5F, 0x00, 0x00, // ! 0x00, 0x07, 0x00, 0x07, 0x00, // " 0x14, 0x7F, 0x14, 0x7F, 0x14, // # 0x24, 0x2A, 0x7F, 0x2A, 0x12, // $ 0x23, 0x13, 0x08, 0x64, 0x62, // % 0x36, 0x49, 0x56, 0x20, 0x50, // & 0x00, 0x08, 0x07, 0x03, 0x00, // ' 0x00, 0x1C, 0x22, 0x41, 0x00, // ( 0x00, 0x41, 0x22, 0x1C, 0x00, // ) 0x2A, 0x1C, 0x7F, 0x1C, 0x2A, // * 0x08, 0x08, 0x3E, 0x08, 0x08, // + 0x00, 0x40, 0x38, 0x18, 0x00, // , 0x08, 0x08, 0x08, 0x08, 0x08, // - 0x00, 0x00, 0x60, 0x60, 0x00, // . 0x20, 0x10, 0x08, 0x04, 0x02, // / 0x3E, 0x51, 0x49, 0x45, 0x3E, // 0 0x00, 0x42, 0x7F, 0x40, 0x00, // 1 0x42, 0x61, 0x51, 0x49, 0x46, // 2 0x21, 0x41, 0x49, 0x4D, 0x33, // 3 0x18, 0x14, 0x12, 0x7F, 0x10, // 4 0x27, 0x45, 0x45, 0x45, 0x39, // 5 0x3C, 0x4A, 0x49, 0x49, 0x30, // 6 0x41, 0x21, 0x11, 0x09, 0x07, // 7 0x36, 0x49, 0x49, 0x49, 0x36, // 8 0x06, 0x49, 0x49, 0x29, 0x1E, // 9 0x00, 0x00, 0x14, 0x00, 0x00, // : 0x00, 0x00, 0x40, 0x34, 0x00, // ; 0x00, 0x08, 0x14, 0x22, 0x41, // < 0x14, 0x14, 0x14, 0x14, 0x14, // = 0x00, 0x41, 0x22, 0x14, 0x08, // > 0x02, 0x01, 0x51, 0x09, 0x06, // ? 0x3E, 0x41, 0x5D, 0x59, 0x4E, // @ 0x7C, 0x12, 0x11, 0x12, 0x7C, // A 0x7F, 0x49, 0x49, 0x49, 0x36, // B 0x3E, 0x41, 0x41, 0x41, 0x22, // C 0x7F, 0x41, 0x41, 0x41, 0x3E, // D 0x7F, 0x49, 0x49, 0x49, 0x41, // E 0x7F, 0x09, 0x09, 0x09, 0x01, // F 0x3E, 0x41, 0x49, 0x49, 0x7A, // G 0x7F, 0x08, 0x08, 0x08, 0x7F, // H 0x00, 0x41, 0x7F, 0x41, 0x00, // I 0x20, 0x40, 0x41, 0x3F, 0x01, // J 0x7F, 0x08, 0x14, 0x22, 0x41, // K 0x7F, 0x40, 0x40, 0x40, 0x40, // L 0x7F, 0x02, 0x1C, 0x02, 0x7F, // M 0x7F, 0x04, 0x08, 0x10, 0x7F, // N 0x3E, 0x41, 0x41, 0x41, 0x3E, // O 0x7F, 0x09, 0x09, 0x09, 0x06, // P 0x3E, 0x41, 0x51, 0x21, 0x5E, // Q 0x7F, 0x09, 0x19, 0x29, 0x46, // R 0x26, 0x49, 0x49, 0x49, 0x32, // S 0x01, 0x01, 0x7F, 0x01, 0x01, // T 0x3F, 0x40, 0x40, 0x40, 0x3F, // U 0x1F, 0x20, 0x40, 0x20, 0x1F, // V 0x3F, 0x40, 0x38, 0x40, 0x3F, // W 0x63, 0x14, 0x08, 0x14, 0x63, // X 0x03, 0x04, 0x78, 0x04, 0x03, // Y 0x61, 0x51, 0x49, 0x45, 0x43, // Z 0x00, 0x7F, 0x41, 0x41, 0x41, // [ 0x02, 0x04, 0x08, 0x10, 0x20, // '\' 0x00, 0x41, 0x41, 0x41, 0x7F, // ] 0x04, 0x02, 0x01, 0x02, 0x04, // ^ 0x80, 0x80, 0x80, 0x80, 0x80, // _ 0x00, 0x03, 0x07, 0x08, 0x00, // ' 0x20, 0x54, 0x54, 0x54, 0x78, // a 0x7F, 0x28, 0x44, 0x44, 0x38, // b 0x38, 0x44, 0x44, 0x44, 0x28, // c 0x38, 0x44, 0x44, 0x28, 0x7F, // d 0x38, 0x54, 0x54, 0x54, 0x18, // e 0x00, 0x08, 0x7E, 0x09, 0x02, // f 0x18, 0xA4, 0xA4, 0xA4, 0x7C, // g 0x7F, 0x08, 0x04, 0x04, 0x78, // h 0x00, 0x44, 0x7D, 0x40, 0x00, // i 0x00, 0x20, 0x40, 0x40, 0x3D, // j 0x00, 0x7F, 0x10, 0x28, 0x44, // k 0x00, 0x41, 0x7F, 0x40, 0x00, // l 0x7C, 0x04, 0x78, 0x04, 0x78, // m 0x7C, 0x08, 0x04, 0x04, 0x78, // n 0x38, 0x44, 0x44, 0x44, 0x38, // o 0xFC, 0x18, 0x24, 0x24, 0x18, // p 0x18, 0x24, 0x24, 0x18, 0xFC, // q 0x7C, 0x08, 0x04, 0x04, 0x08, // r 0x48, 0x54, 0x54, 0x54, 0x24, // s 0x04, 0x04, 0x3F, 0x44, 0x24, // t 0x3C, 0x40, 0x40, 0x20, 0x7C, // u 0x1C, 0x20, 0x40, 0x20, 0x1C, // v 0x3C, 0x40, 0x30, 0x40, 0x3C, // w 0x44, 0x28, 0x10, 0x28, 0x44, // x 0x4C, 0x90, 0x90, 0x90, 0x7C, // y 0x44, 0x64, 0x54, 0x4C, 0x44, // z 0x00, 0x08, 0x36, 0x41, 0x00, // { 0x00, 0x00, 0x77, 0x00, 0x00, // | 0x00, 0x41, 0x36, 0x08, 0x00, // } 0x02, 0x01, 0x02, 0x04, 0x02, // ~ 0x00, 0x06, 0x09, 0x09, 0x06, // degrees }; template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::print(char c) { write(&font[c - 32][0], 5); write(&font[0][0], 1); } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::print(const char *s) { while(*s) { write(&font[*s - 32][0], 5); write(&font[0][0], 1); ++s; } } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::print(const char *s, unsigned char m) { unsigned char c; while(*s) { c = font[*s - 32][0] ^ m; write(&c, 1); c = font[*s - 32][1] ^ m; write(&c, 1); c = font[*s - 32][2] ^ m; write(&c, 1); c = font[*s - 32][3] ^ m; write(&c, 1); c = font[*s - 32][4] ^ m; write(&c, 1); write(&m, 1); ++s; } } template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::printv(unsigned char x, unsigned char y, char *s) { while(*s) { pos(x, y); ++y; write(&font[*s - 32][0], 5); write(&font[0][0], 1); ++s; } } static const unsigned char num11x16[19][11 * 2] = { 0x00,0xF0,0xFC,0xFE,0x06,0x02,0x06,0xFE,0xFC,0xF0,0x00, // 0 0x00,0x07,0x1F,0x3F,0x30,0x20,0x30,0x3F,0x1F,0x07,0x00, 0x00,0x00,0x08,0x0C,0xFC,0xFE,0xFE,0x00,0x00,0x00,0x00, // 1 0x00,0x20,0x20,0x20,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x00, 0x00,0x0C,0x0E,0x06,0x02,0x02,0x86,0xFE,0x7C,0x38,0x00, // 2 0x00,0x30,0x38,0x3C,0x36,0x33,0x31,0x30,0x30,0x38,0x00, 0x00,0x0C,0x0E,0x86,0x82,0x82,0xC6,0xFE,0x7C,0x38,0x00, // 3 0x00,0x18,0x38,0x30,0x20,0x20,0x31,0x3F,0x1F,0x0E,0x00, 0x00,0x00,0xC0,0x20,0x18,0x04,0xFE,0xFE,0xFE,0x00,0x00, // 4 0x00,0x03,0x02,0x02,0x02,0x22,0x3F,0x3F,0x3F,0x22,0x02, 0x00,0x00,0x7E,0x7E,0x46,0x46,0xC6,0xC6,0x86,0x00,0x00, // 5 0x00,0x18,0x38,0x30,0x20,0x20,0x30,0x3F,0x1F,0x0F,0x00, 0x00,0xC0,0xF0,0xF8,0xFC,0x4C,0xC6,0xC2,0x82,0x00,0x00, // 6 0x00,0x0F,0x1F,0x3F,0x30,0x20,0x30,0x3F,0x1F,0x0F,0x00, 0x00,0x06,0x06,0x06,0x06,0x06,0xC6,0xF6,0x3E,0x0E,0x00, // 7 0x00,0x00,0x00,0x30,0x3C,0x0F,0x03,0x00,0x00,0x00,0x00, 0x00,0x38,0x7C,0xFE,0xC6,0x82,0xC6,0xFE,0x7C,0x38,0x00, // 8 0x00,0x0E,0x1F,0x3F,0x31,0x20,0x31,0x3F,0x1F,0x0E,0x00, 0x00,0x78,0xFC,0xFE,0x86,0x02,0x86,0xFE,0xFC,0xF8,0x00, // 9 0x00,0x00,0x00,0x21,0x21,0x31,0x1D,0x1F,0x0F,0x03,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x70,0x70,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // : 0x00,0x0E,0x0E,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // . 0x00,0x38,0x38,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0xC0,0x30,0x0C,0x00,0x00,0x00,0x00, // / 0x00,0x30,0x0C,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80, // - 0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 0x00,0x18,0x3C,0x66,0x66,0x3C,0x18,0x00,0x00,0x00,0x00, // 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xF0,0xF8,0x0C,0x06,0x02,0x02,0x02,0x02,0x0E,0x0C,0x00, // C 0x03,0x07,0x0C,0x18,0x10,0x10,0x10,0x10,0x1C,0x0C,0x00, 0xFE,0xFE,0x42,0x42,0x42,0x42,0x42,0x42,0x00,0x00,0x00, // F 0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0xFE,0xFE,0x40,0xE0,0xB0,0x18,0x0C,0x06,0x02,0x00,0x00, // K 0x1F,0x1F,0x00,0x00,0x01,0x03,0x06,0x0C,0x18,0x10,0x00 }; template volatile unsigned char &_EP, unsigned char _CE, volatile unsigned char &_RP, unsigned char _RST, unsigned _RD> void Nokia7110<_SP, _CLK, _DATA, _CP, _DC, _EP, _CE, _RP, _RST, _RD>::pd12(unsigned n, unsigned x, unsigned y) { pos(x, y); write(num11x16[n], 11, lcd_data); pos(x, ++y); write(num11x16[n] + 11, 11, lcd_data); } } // namespace
  5. Like
    oPossum reacted to greeeg in Casio watch rebuild w/ MSP430   
    Source code is super simple, since the MSP debug stack is doing all the work.
     
    Just quickly compiled it on my PC, looks great! Just spits out a continuous stream of values, which you can pipe into a gnuplot etc.
     
    Turns your launchpad into a current sensor for any (3.6v only) application.
  6. Like
    oPossum reacted to Rickta59 in Question of memory and C++ object allocation   
    Just because you put the String object on the stack that doesn' t mean the managed string is on the stack.  In fact using the code above you end up making multiple calls to new (really malloc in Energias case)
    Breakpoint 1, setup () at /tmp/build7151962017509443788.tmp/sketch_may24a.cpp:2 2 void setup(); (gdb) b malloc Breakpoint 2 at 0xe6da: file ./stdlib/malloc.c, line 32. (gdb) c Continuing. Breakpoint 2, malloc (size=11) at ./stdlib/malloc.c:32 32 ./stdlib/malloc.c: No such file or directory. in ./stdlib/malloc.c (gdb) where #0 malloc (size=11) at ./stdlib/malloc.c:32 Reading 64 bytes from 0x02c0 #1 0x0000e2aa in String::changeBuffer (this=0x2f8, maxStrLen=10) at /mnt/vbox/shared/github/Energia/build/linux/work/hardware/msp430/cores/msp430/WString.cpp:159 #2 0x0000e2ec in String::reserve (this=0x2f8, size=<value optimized out>) at /mnt/vbox/shared/github/Energia/build/linux/work/hardware/msp430/cores/msp430/WString.cpp:150 Reading 8 bytes from 0xe7dc Reading 8 bytes from 0xe7e4 #3 0x0000e31a in String::copy (this=0x2f8, cstr=0xe7dc "0123456789", length=10) at /mnt/vbox/shared/github/Energia/build/linux/work/hardware/msp430/cores/msp430/WString.cpp:177 #4 0x0000e35e in String::String (this=<value optimized out>, cstr=<value optimized out>) at /mnt/vbox/shared/github/Energia/build/linux/work/hardware/msp430/cores/msp430/WString.cpp:33 #5 0x0000e098 in setup () at /tmp/build7151962017509443788.tmp/sketch_may24a.cpp:6 #6 0x0000e05a in main () at /mnt/vbox/shared/github/Energia/build/linux/work/hardware/msp430/cores/msp430/main.cpp:7 (gdb) c Breakpoint 2, malloc (size=1) at ./stdlib/malloc.c:32 32 in ./stdlib/malloc.c (gdb) c Breakpoint 2, malloc (size=5) at ./stdlib/malloc.c:32 32 in ./stdlib/malloc.c As you can see it made at least 3 calls to malloc 11 bytes, 1 byte, and 5 bytes.  The 11, 1, and 5 are requested by the String class.
     
    Although the String class data is going to be put on the stack, that only contains the meta data, like the buffer address, its capacity and len:
    (gdb) p this $5 = (const String * const) 0x2f8 String foo = {buffer = 0x240 "0123456789", capacity = 10, len = 10} The actual allocated buffer comes from heap managed memory, note the address of 0x240 which is much lower address than the address of the String foo metadata living at 0x2f8.  The malloced data represents the actual buffer content length plus a null terminator byte. So 11 bytes 10+1 null and 5 bytes (4 + 1 null).  In the string class the line:
     
    String out; 
     
    ends up allocating 1 byte because an initial value isn't provided so it defaults to "" + null.
     
    In all honesty, you want to avoid using the String class as it is going to eventually fragment memory and fail.  It is a horrible thing to use on chips with hardly any memory.
     
    -rick
     
    BTW: Here is the test code I used:
     
  7. Like
  8. Like
    oPossum got a reaction from artium in An unusual way to print integers   
    A common method of printing integers uses a divide (/) and modulus (%) operation to calculate each digit. This is portable and supports any number base. The string is created in reverse order and the digit generation can easily be terminated when all significant digits have been printed, so additional logic for leading zero suppression is not needed.
     
    A much faster method uses BCD math to generate a BCD value that can then be easily convered to a string. This requires assembly or intrinsic C functions for the most efficient BCD math, so it isn't portable.
     
    The method shown here creates a fixed point binary fraction using a single division operation and then extracts the digits using bit shifts and an add. The division is unrolled and crafted to generate a result with the binary point between words to allow for efficient digit extraction. After division, the upper word contains the first digit and the lower word contains a binary fraction with all other digits. The digits are generated from the fraction by multiplying by 10. This is done with 2 shifts and an add. The result will be in the upper word. Preparation for the next digit is just a matter of clearing the upper word.
    This certainly seems like an awkward method, but it requires far less math than doing division for each digit.
     

    // Print unsigned 16 bit integer with leading zero suppression static void spu16(char * s, uint16_t n) { uint16_t d = 10000 << 2; uint32_t r = 0; if (n >= d) n -= d, r |= (1 << 18); d >>= 1; if (n >= d) n -= d, r |= (1 << 17); n <<= 1; if (n >= d) n -= d, r |= (1 << 16); n <<= 1; if (n >= d) n -= d, r |= (1 << 15); n <<= 1; if (n >= d) n -= d, r |= (1 << 14); n <<= 1; if (n >= d) n -= d, r |= (1 << 13); n <<= 1; if (n >= d) n -= d, r |= (1 << 12); n <<= 1; if (n >= d) n -= d, r |= (1 << 11); n <<= 1; if (n >= d) n -= d, r |= (1 << 10); n <<= 1; if (n >= d) n -= d, r |= (1 << 9); n <<= 1; if (n >= d) n -= d, r |= (1 << 8); n <<= 1; if (n >= d) n -= d, r |= (1 << 7); n <<= 1; if (n >= d) n -= d, r |= (1 << 6); n <<= 1; if (n >= d) n -= d, r |= (1 << 5); n <<= 1; if (n >= d) n -= d, r |= (1 << 4); n <<= 1; if (n >= d) n -= d, r |= (1 << 3); n <<= 1; if (n >= d) n -= d, r |= (1 << 2); r += (1 << 2); unsigned c, z; c = z = (r >> 16); unsigned i = 4; do { if (z || c) *s++ = z = ('0' + c); r &= 0xFFFF; r <<= 1; r += (r << 2); c = (r >> 16); } while (--i); *s++ = '0' + c; *s = 0; } When a 32 bit hardware multiplier is available, the code can be modified to multiply by the reciprocal rather than divide. The multiplication by 10 is now explicit to also take advantage of the hardware multiplier.

    static void spu16(char * s, uint16_t n) { uint32_t r = ((53687UL * n) >> 13) + (1 << 2); unsigned c, z; c = z = (r >> 16); unsigned i = 4; do { if (z || c) *s++ = z = ('0' + c); r = (r & 0xFFFF) * 10; c = (r >> 16); } while (--i); *s++ = '0' + c; *s = 0; }
  9. Like
    oPossum reacted to tonyp12 in Hard intvec rejumping through Soft intvec for Firmware updates   
    #include "msp430.h" #define UART_RX BIT1 #define UART_TX BIT2 #define UART_PORT(x) (P1 ##x) #define softintvec 0xC0004210 // start of flash + opcode for BR void bootloader_reset(void); void TXdata(char* pnt, int len); void bootloader_USCIAB0TX(void); void bootloader_flasherase(void); __interrupt void bootloaderRX_ISR(void); //-------------------- define structures -------------------- struct jumpstruct { const unsigned long branch[16]; const unsigned int bsl; const unsigned long* vector[16]; }; //-------------------- declare strings ---------------------- #pragma location = "BOOTSTRINGS" const char uartstring1[] = "run main, password anytime erases\r\n"; #pragma location = "BOOTSTRINGS" const char uartstring2[] = "Ready for Firmware\r\n"; //-------------------- reserve a fixed ram location --------- #pragma location = 0x200 __no_init char* passwordpnt; //-------------------- entry on reset ----------------------- #pragma location = "BOOTLOADER" void bootloader_reset(void) { asm(" MOV #SFE(CSTACK), SP"); // set stack pointer WDTCTL = WDTPW+WDTHOLD; if (CALBC1_1MHZ != 0xff){ // erased by mistake? BCSCTL1 = CALBC1_1MHZ; // Set DCO to factory calibrate 1MHz DCOCTL = CALDCO_1MHZ; } UCA0CTL1 = UCSWRST; // Set UCSWRST (hold USCI in Reset state) UCA0CTL1 |= UCSSEL_2; // Use SMCLK UCA0CTL0 = 0; // UART N,8,1 UCA0BR0 = 104; // upper register (9600) UCA0BR1 = 0; // the lower rate register UCA0MCTL = UCBRS1; // don't use modulation as it jitters UART_PORT(SEL) |= UART_TX + UART_RX; UART_PORT(SEL2) |= UART_TX + UART_RX; UCA0CTL1 &= ~UCSWRST; // relase reset, Initialize USCI state machine IE2 |= UCA0RXIE; // enable USCI_A0 RX interrupt if (*(int*)0xC01E != 0xffff){ // is reset vector filled in? TXdata((char*)&uartstring1, sizeof(uartstring1)-1); passwordpnt = (char*) &uartstring1; // use string1 as password __bis_SR_register(GIE); // so our RX_ISR can get data asm (" BR &0xC01E"); // indirect jump } TXdata((char*)&uartstring2, sizeof(uartstring2)-1); BCSCTL3 |= LFXT1S_2; // aclk = vloclk BCSCTL1 |= DIVA_3; // aclk divided by 8 WDTCTL = WDTPW+WDTCNTCL+WDTSSEL; // aclk sourced ~28sec __bis_SR_register(LPM3_bits); // trap it } //-------------------- uart byte tx routine ----------------- #pragma location = "BOOTLOADER" void TXdata(char* pnt, int len) { while(len--){ UCA0TXBUF = *pnt++; while (!(IFG2 & UCA0TXIFG)); } } //-------------------- UartRX sniffer ISR ------------------- #pragma location = "BOOTLOADER" __interrupt void bootloaderRX_ISR(void){ if (IFG2 & UCA0RXIFG){ // our USCI_A0 Recive Interrupt? if (UCA0RXBUF == *passwordpnt){ // a match? if (++passwordpnt == uartstring1+sizeof(uartstring1)-3){ bootloader_flasherase(); WDTCTL = WDTPW+WDTCNTCL+WDTSSEL; // reset the system __bis_SR_register_on_exit(LPM3_bits); } } else passwordpnt = (char*) &uartstring1; // reset it to the start if (*(int*)0xC00E != 0xffff){ // is USCIAB0RX vector filled in? IFG2 |= UCA0RXIFG; // if real routine needs it asm (" BR &0xC00E"); // indirect jump to user isr } } } //-------------------- flash erase blocks ------------------- #pragma location = "BOOTLOADER" void bootloader_flasherase(void){ int* pnt = (int*) 0xC000; // start of flash unsigned int i = 16*2-1; // erase 16K -512B WDTCTL = WDTPW+WDTHOLD; // no WDT __bic_SR_register(GIE); // so no interupt FCTL2 = FWKEY + FSSEL0 + FN1; // MCLK/3 for Flash Timing Generator FCTL3 = FWKEY; // Clear Lock bit while (i--){ FCTL1 = FWKEY + ERASE; // Set Erase bit *pnt = 0; // Dummy write to erase Flash block pnt += 256; // 256 ints up (e.g 512 bytes) } FCTL3 = FWKEY + LOCK; // Set LOCK bit } //-------------------- const rejumping real intvec ---------- #pragma location = "JUMPTABLE" const struct jumpstruct jumptable = { .branch[0] = softintvec + 0 * 0x20000u, .branch[1] = softintvec + 1 * 0x20000u, .branch[2] = softintvec + 2 * 0x20000u, .branch[3] = softintvec + 3 * 0x20000u, .branch[4] = softintvec + 4 * 0x20000u, .branch[5] = softintvec + 5 * 0x20000u, .branch[6] = softintvec + 6 * 0x20000u, .branch[7] = softintvec + 7 * 0x20000u, .branch[8] = softintvec + 8 * 0x20000u, .branch[9] = softintvec + 9 * 0x20000u, .branch[10] = softintvec + 10 * 0x20000u, .branch[11] = softintvec + 10 * 0x20000u, .branch[12] = softintvec + 12 * 0x20000u, .branch[13] = softintvec + 13 * 0x20000u, .branch[14] = softintvec + 14 * 0x20000u, .branch[15] = softintvec + 15 * 0x20000u, .bsl = 0x0000, .vector[0] = &jumptable.branch[0], .vector[1] = &jumptable.branch[1], .vector[2] = &jumptable.branch[2], .vector[3] = &jumptable.branch[3], .vector[4] = &jumptable.branch[4], .vector[5] = &jumptable.branch[5], .vector[6] = &jumptable.branch[6], .vector[7] = (unsigned long*) &bootloaderRX_ISR, .vector[8] = &jumptable.branch[8], .vector[9] = &jumptable.branch[9], .vector[10] = &jumptable.branch[10], .vector[11] = &jumptable.branch[11], .vector[12] = &jumptable.branch[12], .vector[13] = &jumptable.branch[13], .vector[14] = &jumptable.branch[14], .vector[15] = (unsigned long*) &bootloader_reset }; #pragma required=jumptable // tell compiler it's not a dead var Override linker location, copy it and edit the last sections so it looks like thisit moves intvec to just before code so firmware is just now one block including the vectors
    -Z(CONST)DATA16_C,DATA16_ID,TLS16_ID,DIFUNCT,CHECKSUM=C020-FDFF // normally C000-FFDD // ------------------------------------- // Code // -Z(CODE)CSTART,ISR_CODE,CODE_ID=C020-FDFF // normally C000-FFDD -P(CODE)CODE=C020-FDFF // normally C000-FFDD // ------------------------------------- // Signature memory and interrupt vectors // -Z(CONST)SIGNATURE=FFDE-FFDF -Z(CODE)INTVEC=C000-C01F // normally FFE0-FFFF -Z(CODE)RESET=C01E-C01F // normally FFFE-FFFF -Z(CODE)BOOTLOADER=FE00-FF5F // new addition -Z(CONST)BOOTSTRINGS=FF60-FF9D // new addition -Z(CONST)JUMPTABLE=FF9E-FFFF // new addition ?Done with  IAR Workbench for G2553
    The bootloaders Uart RX routine sniffs all incoming data so user can share uart for its intended purpose,
     
    so if you have a Cellular GSM module and you want remote firmware update capability
    ?this may be best near un-brickable way, as soon it detects the password it erase the Flash (except the boot loader block)
    and now it will go in to a waiting sequence for new firmware, It will send a text string ~28second to remind you.
    ?The firmware Flash Loader itself is not done yet, probably need to move up to a 1K size block to fit everything
  10. Like
    oPossum reacted to tonyp12 in Hard intvec rejumping through Soft intvec for Firmware updates   
    The idea is that any new firmware does not include the upper 512bytes of Flash,
    but as IRQ vectors will change with new firmware but is now part of the firmware-block as compiler Intvec have been moved -512 bytes down.
    If you put your custom Loader in the upper 512bytes too and as you never erase this block, much less chance of bricking.
     
    I guess opcode for BR could change from msp family? but this does work on a G2553.
    ?CCS could use pragma location if it can not handle @
     
    Not all vectors are available or used, so you should fill them with random value instead for the BSL password if you want some security.
    But at least the BSL password will now stay the same from reversions.
    static const unsigned int jumptable[49] @0xFF9E = {   0x4210,0xFDE0,     // indirect BR &0x----   0x4210,0xFDE2,   0x4210,0xFDE4,   0x4210,0xFDE6,   0x4210,0xFDE8,   0x4210,0xFDEA,   0x4210,0xFDEC,   0x4210,0xFDEE,   0x4210,0xFDF0,   0x4210,0xFDF2,   0x4210,0xFDF4,   0x4210,0xFDF6,   0x4210,0xFDF8,   0x4210,0xFDFA,   0x4210,0xFDFC,   0x4210,0xFDFE,   0x0000,            // BSL signature   0xFF9E,0xFFA2,     // real intvec   0xFFA6,0xFFAA,   0xFFAE,0xFFB2,   0xFFB6,0xFFBA,   0xFFBE,0xFFC2,   0xFFC6,0xFFCA,   0xFFCE,0xFFD2,   0xFFD6,0xFFDA  }; int main( void ) {    WDTCTL = WDTPW + WDTHOLD;    // Stop watchdog timer   if (jumptable[0] == 0xffff) {while(1){};}; // need to use array for something Override default location of linker file and make a copy of it and move it to local project folder and edit:
    // ------------------------------------- // Signature memory and interrupt vectors //   -Z(CONST)SIGNATURE=FFDE-FFDF  // used by BSL -Z(CODE)INTVEC=FDE0-FDFF      // normally FFE0-FFFF -Z(CODE)RESET=FDFE-FDFF       // normally FFFE-FFFF
  11. Like
    oPossum got a reaction from agaelema in Tiny printf() - C version   
    This is a tiny printf() function that can be used with the chips that come with the Launchpad. Code size is about 640 bytes with CCS.
     
    There are 7 format specifiers:
    %c - Character
    %s - String
    %i - signed Integer (16 bit)
    %u - Unsigned integer (16 bit)
    %l - signed Long (32 bit)
    %n - uNsigned loNg (32 bit)
    %x - heXadecimal (16 bit)
     
    Field width, floating point and other standard printf() features are not supported.
     
    printf() code

    #include "msp430g2231.h" #include "stdarg.h" void putc(unsigned); void puts(char *); static const unsigned long dv[] = { // 4294967296 // 32 bit unsigned max 1000000000, // +0 100000000, // +1 10000000, // +2 1000000, // +3 100000, // +4 // 65535 // 16 bit unsigned max 10000, // +5 1000, // +6 100, // +7 10, // +8 1, // +9 }; static void xtoa(unsigned long x, const unsigned long *dp) { char c; unsigned long d; if(x) { while(x < *dp) ++dp; do { d = *dp++; c = '0'; while(x >= d) ++c, x -= d; putc(c); } while(!(d & 1)); } else putc('0'); } static void puth(unsigned n) { static const char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; putc(hex[n & 15]); } void printf(char *format, ...) { char c; int i; long n; va_list a; va_start(a, format); while(c = *format++) { if(c == '%') { switch(c = *format++) { case 's': // String puts(va_arg(a, char*)); break; case 'c': // Char putc(va_arg(a, char)); break; case 'i': // 16 bit Integer case 'u': // 16 bit Unsigned i = va_arg(a, int); if(c == 'i' && i < 0) i = -i, putc('-'); xtoa((unsigned)i, dv + 5); break; case 'l': // 32 bit Long case 'n': // 32 bit uNsigned loNg n = va_arg(a, long); if(c == 'l' && n < 0) n = -n, putc('-'); xtoa((unsigned long)n, dv); break; case 'x': // 16 bit heXadecimal i = va_arg(a, int); puth(i >> 12); puth(i >> 8); puth(i >> 4); puth(i); break; case 0: return; default: goto bad_fmt; } } else bad_fmt: putc(c); } va_end(a); }
     
    test code

    #include "msp430g2231.h" void serial_setup(unsigned out_mask, unsigned in_mask, unsigned duration); void printf(char *, ...); void main(void) { char *s; char c; int i; unsigned u; long int l; long unsigned n; unsigned x; // Disable watchdog WDTCTL = WDTPW + WDTHOLD; // Use 1 MHz DCO factory calibration DCOCTL = 0; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; // Setup the serial port // Serial out: P1.1 (BIT1) // Serial in: P1.2 (BIT2) // Bit rate: 9600 (CPU freq / bit rate) serial_setup(BIT1, BIT2, 1000000 / 9600); printf("%s", "\r\n*** printf() test ***\r\n"); s = "test"; c = 'X'; i = -12345; u = 12345; l = -1234567890; n = 1234567890; x = 0xABCD; printf("String %s\r\n", s); printf("Char %c\r\n", c); printf("Integer %i\r\n", i); printf("Unsigned %u\r\n", u); printf("Long %l\r\n", l); printf("uNsigned loNg %n\r\n", n); printf("heX %x\r\n", x); printf("multiple args %s %c %i %u %l %n %x\r\n", s, c, i, u, l, n, x); printf("\r\n*** Done ***\r\n"); for(;; }

  12. Like
    oPossum reacted to tonyp12 in tiny msp430 preemptive multitasking system   
    ?{28}; is the same thing as {28,0,0} and 28 was just random number to show something
    so if you forget to fill in all stack sizes for each task I will give you 24 words (48bytes)
     
    A minimum should  be 14 words if your task don't use the stack at all.
     
    -26, as I cast the address to (int) it's now a byte space referenced
    If I wrapped  it first
    (int) (multistack-13); it will be word referenced as it is a int pointer, probably should wrap it for next time,
    ?So I'm just adjusting the stack pointer to include the PC,SR and the 12 pop's it will see on first entry (only done for task2+)
    That is 14 words but Pop is post increment, it works if I do  -13 words
    the registers will have random data first time, but as task have not started doing anything yet it should not care.

    a POP R15 is actual a: MOV.W @R1+,R15
  13. Like
    oPossum reacted to tonyp12 in tiny msp430 preemptive multitasking system   
    Tested on G2553 Launchpad with IAR, I recommend G2955 with 1K RAM if you want more than 3 task
    #include "msp430.h" #include "common.h" //=========================(C) Tony Philipsson 2016 ======================= funcpnt const taskpnt[]={ task1, task2, task3, // <- PUT YOUR TASKS HERE }; const int stacksize[tasks] = {28}; // a blank value defaults to 24 stack words //========================================================================= int taskstackpnt[tasks]; unsigned int taskdelay[tasks]; char taskrun; int main( void ) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer if (CALBC1_8MHZ != 0xff){ // erased by mistake? BCSCTL1 = CALBC1_8MHZ; // Set DCO to factory calibrate 1MHz DCOCTL = CALDCO_8MHZ; } int* multistack = (int*) __get_SP_register(); int i=0; while(i<tasks-1){ int j = stacksize[i]; if (!j) j = 24; multistack -= j; *(multistack) = (int) taskpnt[++i]; // prefill in PC *(multistack-1) = GIE; // prefill in SR taskstackpnt[i] = (int) multistack-26; // needs 12 dummy push words } WDTCTL = WDTPW+WDTTMSEL+WDTCNTCL; // 4ms interval at 8MHz smclk IE1 |= WDTIE; __bis_SR_register(GIE); asm ("br &taskpnt"); // indirect jmp to first task } //============= TASK SWITCHER ISR ============= #pragma vector = WDT_VECTOR __raw __interrupt void taskswitcher(void) { asm ("push R15\n push R14\n push R13\n push R12\n" "push R11\n push R10\n push R9\n push R8\n" "push R7\n push R6\n push R5\n push R4"); taskstackpnt[taskrun] = __get_SP_register(); if (++taskrun == tasks) taskrun = 0; __set_SP_register(taskstackpnt[taskrun]); asm ("pop R4\n pop R5\n pop R6\n pop R7\n" "pop R8\n pop R9\n pop R10\n pop R11\n" "pop R12\n pop R13\n pop R14\n pop R15"); } #include "msp430.h" #include "common.h" __task void task1(void){ P1DIR |= BIT0; while(1){ __delay_cycles(800000); P1OUT |= BIT0; __delay_cycles(800000); P1OUT &=~BIT0; } } #include "msp430.h" #include "common.h" __task void task2(void){ P1DIR |= BIT6; while(1){ __delay_cycles(1200000); P1OUT |= BIT6; __delay_cycles(1200000); P1OUT &=~BIT6; } } #include "msp430.h" #include "common.h" unsigned int fibo(int); __task void task3(void){ int temp = 0; while(1){ fibo(++temp); } } unsigned int fibo(int n){ if (n < 2) return n; else return (fibo(n-1) + fibo(n-2)); } #ifndef COMMON_H_ #define COMMON_H_ #define tasks (sizeof(taskpnt)/2) __task void task1(void); __task void task2(void); __task void task3(void); typedef __task void (*funcpnt)(void); #endif
  14. Like
    oPossum reacted to monsonite in SIMPL - A Tiny Language for MSP430   
    Hi All
     
    Back in May 2013, I came across a piece of development work by Ward Cunningham (wiki inventor) for a tiny interpreted language "Txtzyme" that ran on Arduino - or virtually any other micro that was supported by a C compiler.
     
    (The name Txtzyme comes from text and enzyme -  an enzyme being a biological catalyst - or a substance that causes (chemical) change to occur faster).
     
    I was intrigued how under 100 lines of C code formed such a useful and extendable language - that I quickly had it running on an Arduino, and was making my own modifications and improvements.  I have since built on Ward's work and called my project SIMPL - Serial Interpreted Microcontroller Language.
     
    (Link to Ward's original work is here  https://github.com/WardCunningham/Txtzyme )
     
    Ward had created a "nano interpreter" in just a few lines of C code, which formed the heart of an interactive language for controlling hardware.
     
    Through just a dozen commands it would allow port manipulation,  flashing LEDs,  musical tone generation, PWM, printing to screen and sensing analogue signals. The language communicated through a uart port to the PC.
     
    Originally Txtzyme offered just these commands - see Ward Cunningham's Read Me:

    a-f      Select an I/O port
    h        help - a summary of commands
    i         input
    k        a loop counter - which decrements each time around the loop  see {}
    m       millisecond delay
    o       output
    p       print the value of variable x followed by carriage return/line feed
    s       sample an ADC channel
    u       microsecond delay
    x       a 16 bit integer variable
    {}     code between these braces is repeated as a loop
    _ _    characters between the underscores are printed to the terminal
     
    Txtzyme was small, entensible and portable - it will run on almost any microcontroller.  The Txtzyme scripts were very small, human readable and easy to send from one device to another.
     
    One early application was for the Arduino to be used as an I/O controller board attached to a Raspberry Pi.  The Pi would send a simple command via it's uart and the Arduino would perform an I/O function, sequence or whatever.
     
    Some months later, I decided to try Txtzyme with the MSP430, as I had got a LaunchPad and Energia had just appeared on my radar. 
    So I took the original Txtzyme Arduino  sketch, loaded into Energia - and behold it worked first time.  Txtzyme was a universal "lingua franca" that could be used on any micro.
     
    If you want to try  arecent version of SIMPL that runs on a LaunchPad MSP430G2553 - here is the Github Gist
     
    https://gist.github.com/anonymous/034dd41f108263d09803
     
    Since then I have ported it to  AVR Cortex M3, M4,  and several soft-core processors running on FPGAs.
     
    Currently my aim is to reduce the SIMPL Kernel to approximately 2kbytes - so that it can become a "Smart Bootloader"  - residing on the microcontroller in the bootloader area, and being immediately available as an interactive toolkit for exercising hardware and performing small applications.
     
    I have written extensively about it in my blog  - starting here  from May 2013
     
    http://sustburbia.blogspot.co.uk/2013/05/txtzyme-minimal-interpreter-and.html
     
    I have also just this week started a wiki - so that all the new information can reside in 1 place.
     
    https://simplex1.wiki.zoho.com/HomePage.html
     
     
    Please get in touch if you would like to know more about SIMPL and my project aims.  
     
     
     
     
    Ken 
     
    London
     
     
     
     
     
     
  15. Like
    oPossum got a reaction from cde in LED Animations / Effects   
    By your command.
     

    larson_dual.c
  16. Like
    oPossum got a reaction from zeke in Console with One-Wire Temperature on MSP430F5529 LP   
    This is obviously wrong:
     

    UCSCTL2 |= 499; // Set DCO Multiplier for 16MHz // (N + 1) * FLLRef = Fdco // (499 + 1) * 32768 = 16MHz change to... 

    UCSCTL2 |= 499; // Set DCO Multiplier for 16MHz // (N + 1) * FLLRef = Fdco // (499 + 1) * 32768 = 16.384 MHz and also change... 

    const unsigned long smclk_freq = 16384000UL; // SMCLK frequency in hertz and delete (or fix and make use of)... 

    /******************************************************************************* * ******************************************************************************/ #define F_CPU 16000000UL #define BAUD 19200 #define BAUD_PRESCALE (((F_CPU / (BAUD * 16UL))) - 1)
  17. Like
    oPossum reacted to cde in Want to learn how to use an Oscilloscope? What if a Msp430 Launchpad was involved!   
    Stumbled upon this. Tektronix, a producer of various engineering test equipment, including Oscilloscopes, happens to have a free to use/download set of courses for learning how to use an Oscilloscope.
     
    The more important part is that it uses a standard MSP430 launchpad + Energia to do it.
     
    http://www.tek.com/lab-course/learning-oscilloscope-operation-using-msp430-launchpad-board
     
     
    The search shows about 20 of the courses:
    http://www.tek.com/courseware/search?body=msp430&field_courseware_categories_value=1902&field_courseware_keywords_value=All&field_course_language_value=&uid=
     
    It's a marketing tool, but it's generic enough for any scope. They also have *shudders* arduino versions as well.
     
  18. Like
    oPossum got a reaction from Rickta59 in Tiny printf() - C version   
    This code was written when the MSP430 Lauchpad shipped with chips that had 128 bytes of RAM, 2k of flash, and no hardware serial port. So it was important to keep code very compact and a software UART was needed.
     
    Your F6137 chip has many hardware UARTS - so it would be wise to use them rather than software UART. Just write a putc() function that sends a char to the UART and you don't need the assembly code.
  19. Like
    oPossum reacted to greeeg in Tiny printf() - C version   
    @@djarami I took a look at your code changes.
     
    Seems that you have fallen into a rookie mistake. The original code made use of ACLK, (with a 32kHz crystal)
    You want to use the code without an external crystal, so you've switch the code to use the internal 1MHz DCO. running into SCLK.
     
    Unfortunately the LPM3 disables this clock source, since it uses more power than ACLK. So you need to alter the sleep mode to LPM0, which just disables the CPU.
    __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 The code is working on my bench with this one modification, when you press 't' in your console you should see some printf formatted statements get printed to screen.
  20. Like
    oPossum got a reaction from agaelema in Faster printing of single precision floating point   
    Printf supports the %e, %f and %g format specifiers for printing floating point numbers. Due to the automatic type promotion of varadic functions in C, the printf code must always print double precision floating point. This makes printing single precision floating point numbers slower than optimal. The precision rounding done by printf also imposes a speed penalty. The code presented here will print single precision floating point numbers much faster than printf - up to 75 times faster. It allows the number of significant digits to be 3 to 8 (a maximum of 7 is recommended). The printed number will be normalized to the range to 1.0 to less than 1000.0 and the appropriate SI prefix appended. This is similar to what the printf %e format does (1.0 to less than 10.0), but a bit easier to interpret. The full range of single precision float can not be represented by SI prefixes, so very small and very large numbers will have '?' as the prefix. This code is intended for display only. It should not be used to store values in a file for later conversion back to float due to limitations in rounding and range.
     
    A brief explanation of the code.
    Do a bitwise conversion of the float to a 32 bit unsigned integer. This is done with by getting the address of the float, casting to an unsigned integer pointer, and then dereferencing the pointer. Simply casting to an unsigned integer would not produce a bitwise copy.

    uint32_t s = *(uint32_t *)&f; The msb contains the sign flag. Append a '-' to the string if the floating point number is negative.
    if (s & (1UL << 31)) *a++ = '-'; Extract the 8 bit exponent.
    int e = (s >> 23) & 0xFF; Move the significand to the upper 24 bits. Set the msb to 0.
    s <<= 8; s &= ~(1UL << 31); An exponent of 255 is used for special values of NaN (not a number) and infinity. Handle these special cases and return.
    if (e == 255) { if (s) { strcpy(a, "NaN"); } else { strcpy(a, "Inf"); } return; An exponent of 0 is used to represent a value of 0 and for denormals. A value of 0 is handled by setting the exponent to 127 - the same exponent used to represent 1.0, and leaving the significand as 0. Denormal numbers do not have the implicit msb of 1, so they are normalized by shifting until the leading 1 is in the msb position.
    } else if (e == 0) { if (s) { e = 1; while (!(s & (1UL << 31))) s <<= 1, --e; } else { e = 127; } If the exponent is some other value, then set the msb of the significand.
    } else { s |= (1UL << 31); } Setup a pointer to the SI prefix. This will be adjusted as the value is normalized.
    char const * sp = "???????yzafpnum kMGTPEZY????" + 15; If the value is less than 1.0 it must be multiplied by 1000 until it is 1.0 or greater. Multiplication by 1000 is done by an implicit multiply by 1024 and then subtracting a multiply by 16 and a multiply by 8.
    if (e < 127) { do { s = s - (s >> 6) - (s >> 7); if (!(s & (1UL << 31))) s <<= 1, --e; e += 10; --sp; } while (e < 127); If the value is 1000.0 or more it must be divided by 1000 until it is less than 1000.0. An unrolled floating point divide is used for maximum speed.
    } else if (e > 135) { while (e > (126 + 10) || (e == (126 + 10) && s >= (1000UL << (32 - 10)))) { uint32_t n = s; s = 0; uint32_t d = 1000UL << (32 - 10); if (n >= d) n -= d, s |= (1UL << 31); d >>= 1; if (n >= d) n -= d, s |= (1UL << 30); d >>= 1; if (n >= d) n -= d, s |= (1UL << 29); d >>= 1; if (n >= d) n -= d, s |= (1UL << 28); d >>= 1; if (n >= d) n -= d, s |= (1UL << 27); d >>= 1; if (n >= d) n -= d, s |= (1UL << 26); d >>= 1; if (n >= d) n -= d, s |= (1UL << 25); d >>= 1; if (n >= d) n -= d, s |= (1UL << 24); d >>= 1; if (n >= d) n -= d, s |= (1UL << 23); d >>= 1; if (n >= d) n -= d, s |= (1UL << 22); d >>= 1; if (n >= d) n -= d, s |= (1UL << 21); d >>= 1; if (n >= d) n -= d, s |= (1UL << 20); d >>= 1; if (n >= d) n -= d, s |= (1UL << 19); d >>= 1; if (n >= d) n -= d, s |= (1UL << 18); d >>= 1; if (n >= d) n -= d, s |= (1UL << 17); d >>= 1; if (n >= d) n -= d, s |= (1UL << 16); d >>= 1; if (n >= d) n -= d, s |= (1UL << 15); d >>= 1; if (n >= d) n -= d, s |= (1UL << 14); d >>= 1; if (n >= d) n -= d, s |= (1UL << 13); d >>= 1; if (n >= d) n -= d, s |= (1UL << 12); d >>= 1; if (n >= d) n -= d, s |= (1UL << 11); d >>= 1; if (n >= d) n -= d, s |= (1UL << 10); d >>= 1; if (n >= d) n -= d, s |= (1UL << 9); d >>= 1; if (n >= d) n -= d, s |= (1UL << 8); d >>= 1; if (n >= d) s += (1UL << 8); if (!(s & (1UL << 31))) s <<= 1, --e; e -= 9; ++sp; } The divide code is quite time consuming, so it would be advantageous to quickly reduce very large numbers. A divide by 1,000,000,000,000 is used to improve performance for these large numbers.The preceding multiply code could do the same for very small numbers, but there is no speed advantage due to the multiply by 1000 using 2 shift/subtract operations and multiply by lager values requiring more than 2 per 1000.

    while (e > (150 + 16) || (e == (150 + 16) && s > (999999995904ULL >> 16))) { uint64_t n = s; n <<= 32; s = 0; uint64_t d = 1000000000000ULL << (64 - 40); if (n >= d) n -= d, s |= (1UL << 31); d >>= 1; if (n >= d) n -= d, s |= (1UL << 30); d >>= 1; if (n >= d) n -= d, s |= (1UL << 29); d >>= 1; if (n >= d) n -= d, s |= (1UL << 28); d >>= 1; if (n >= d) n -= d, s |= (1UL << 27); d >>= 1; if (n >= d) n -= d, s |= (1UL << 26); d >>= 1; if (n >= d) n -= d, s |= (1UL << 25); d >>= 1; if (n >= d) n -= d, s |= (1UL << 24); d >>= 1; if (n >= d) n -= d, s |= (1UL << 23); d >>= 1; if (n >= d) n -= d, s |= (1UL << 22); d >>= 1; if (n >= d) n -= d, s |= (1UL << 21); d >>= 1; if (n >= d) n -= d, s |= (1UL << 20); d >>= 1; if (n >= d) n -= d, s |= (1UL << 19); d >>= 1; if (n >= d) n -= d, s |= (1UL << 18); d >>= 1; if (n >= d) n -= d, s |= (1UL << 17); d >>= 1; if (n >= d) n -= d, s |= (1UL << 16); d >>= 1; if (n >= d) n -= d, s |= (1UL << 15); d >>= 1; if (n >= d) n -= d, s |= (1UL << 14); d >>= 1; if (n >= d) n -= d, s |= (1UL << 13); d >>= 1; if (n >= d) n -= d, s |= (1UL << 12); d >>= 1; if (n >= d) n -= d, s |= (1UL << 11); d >>= 1; if (n >= d) n -= d, s |= (1UL << 10); d >>= 1; if (n >= d) n -= d, s |= (1UL << 9); d >>= 1; if (n >= d) n -= d, s |= (1UL << 8); //d >>= 1; //if (n >= d) s += (1UL << 8); if (n) s += (1UL << 8); if (!(s & (1UL << 31))) s <<= 1, --e; e -= 39; sp += 4; } Rounding is the most difficult part of printing floating point numbers. Precalculated float constants are applied based on the value of the float and the specified number of significant digits. This simple method is fast and allows for good results for up to 7 significant digits.
    typedef struct { uint32_t s; int e; } TFR; TFR const r[] = { 0x800000UL << 7, 126 + 1, // 0.5 0xCCCCCDUL << 7, 122 + 1, // 0.05 0xA3D70AUL << 7, 119 + 1, // 0.005 0x83126FUL << 7, 116 + 1, // 0.0005 0xD1B717UL << 7, 112 + 1, // 0.00005 0xA7C5ACUL << 7, 109 + 1, // 0.000005 0x8637BDUL << 7, 106 + 1, // 0.0000005 0xD6BF95UL << 7, 102 + 1 // 0.00000005 }; if (d < 3) d = 3; else if (d > 8) d = 8; if (s) { TFR const *pr = &r[d - 3]; if (e < (126 + 4) || (e == (126 + 4) && s < (10UL << (32 - 4)))) { // < 10 pr += 2; } else if (e < (126 + 7) || (e == (126 + 7) && s < (100UL << (32 - 7)))) { // < 100 ++pr; } s += (pr->s >> (e - pr->e)); if (e == (126 + 10) && s >= (1000UL << (32 - 10))) s = (1UL << 31), e = 127, ++sp; else if (!(s & (1UL << 31))) s >>= 1, s |= (1UL << 31), ++e; } The integer part is printed using iterative subtraction of base 10 constants. This is typically faster than the common divide/modulus method.
    unsigned i = s >> 16; i >>= (136 - e); unsigned id = 1; char c; if (i >= (100 << 6)) { ++id; c = '0'; while (i >= (100 << 6)) i -= (100 << 6), ++c; *a++ = c; } if (id == 2 || i >= (10 << 6)) { ++id; c = '0'; while (i >= (10 << 6)) i -= (10 << 6), ++c; *a++ = c; } c = '0'; while (i >= (1 << 6)) i -= (1 << 6), ++c; *a++ = c; The fractional part is printed by iterative multiplication by 10.
    *a++ = '.'; if (e < 130) s >>= (130 - e); else s <<= (e - 130); d -= id; while (d) { s &= ((1UL << 28) - 1); s = (s << 3) + (s << 1); *a++ = '0' + (s >> 28); --d; } The SI prefix is appended and the string is terminated.
    *a++ = *sp; *a = 0; The resulting performance increase was more than I expected. 

    TI 4.4.4 GCC 4.9.1 --------------------------------------- %e 16402 6.47 s non-functional %f 16380 5.78 s non-functional %g 16402 4.69 s non-functional ftoas(7) 8480 0.12 s 11892 0.27 s ftoas(3) 8480 0.09 s 11892 0.17 s Results of the test code using ftoas(7), %e, %g, and %f
    0.000000 0.000000e+00 0 0.000000 1.401298? 0.000000e+00 0 0.000000 1.401298? 0.000000e+00 0 0.000000 9.809089? 0.000000e+00 0 0.000000 99.49219? 0.000000e+00 0 0.000000 1.000527? 0.000000e+00 0 0.000000 9.999666? 0.000000e+00 0 0.000000 99.99946? 0.000000e+00 0 0.000000 1.000000? 0.000000e+00 0 0.000000 12.00000? 1.200000e-38 1.2e-38 0.000000 100.0000? 1.000000e-37 1e-37 0.000000 1.000000? 1.000000e-36 1e-36 0.000000 10.00000? 1.000000e-35 1e-35 0.000000 100.0000? 1.000000e-34 1e-34 0.000000 1.000000? 1.000000e-33 1e-33 0.000000 10.00000? 1.000000e-32 1e-32 0.000000 100.0000? 1.000000e-31 1e-31 0.000000 1.000000? 1.000000e-30 1e-30 0.000000 10.00000? 1.000000e-29 1e-29 0.000000 100.0000? 1.000000e-28 1e-28 0.000000 1.000000? 1.000000e-27 1e-27 0.000000 10.00000? 1.000000e-26 1e-26 0.000000 100.0000? 1.000000e-25 1e-25 0.000000 1.000000y 1.000000e-24 1e-24 0.000000 10.00000y 1.000000e-23 1e-23 0.000000 100.0000y 1.000000e-22 1e-22 0.000000 1.000000z 1.000000e-21 1e-21 0.000000 10.00000z 1.000000e-20 1e-20 0.000000 100.0000z 1.000000e-19 1e-19 0.000000 1.000000a 1.000000e-18 1e-18 0.000000 10.00000a 1.000000e-17 1e-17 0.000000 100.0000a 1.000000e-16 1e-16 0.000000 1.000000f 1.000000e-15 1e-15 0.000000 10.00000f 1.000000e-14 1e-14 0.000000 100.0000f 1.000000e-13 1e-13 0.000000 1.000000p 1.000000e-12 1e-12 0.000000 10.00000p 1.000000e-11 1e-11 0.000000 100.0000p 1.000000e-10 1e-10 0.000000 1.000000n 1.000000e-09 1e-09 0.000000 10.00000n 1.000000e-08 1e-08 0.000000 100.0000n 1.000000e-07 1e-07 0.000000 1.000000u 1.000000e-06 1e-06 0.000001 10.00000u 1.000000e-05 1e-05 0.000010 100.0000u 1.000000e-04 0.0001 0.000100 1.000000m 1.000000e-03 0.001 0.001000 10.00000m 1.000000e-02 0.01 0.010000 100.0000m 1.000000e-01 0.1 0.100000 1.000000 1.000000e+00 1 1.000000 1.234568 1.234568e+00 1.23457 1.234568 10.00000 1.000000e+01 10 10.000000 100.0000 1.000000e+02 100 100.000000 1.000000k 1.000000e+03 1000 1000.000000 10.00000k 1.000000e+04 10000 10000.000000 100.0000k 1.000000e+05 100000 100000.000000 1.000000M 1.000000e+06 1e+06 1000000.000000 10.00000M 1.000000e+07 1e+07 10000000.000000 100.0000M 1.000000e+08 1e+08 100000000.000000 1.000000G 1.000000e+09 1e+09 1000000000.000000 10.00000G 1.000000e+10 1e+10 10000000000.000000 100.0000G 1.000000e+11 1e+11 99999997952.000010 1.000000T 1.000000e+12 1e+12 999999995903.999925 10.00000T 1.000000e+13 1e+13 9999999827968.000174 100.0000T 1.000000e+14 1e+14 100000000376832.008362 1.000000P 1.000000e+15 1e+15 999999986991104.125977 10.00000P 1.000000e+16 1e+16 10000000272564222.812653 100.0000P 1.000000e+17 1e+17 99999998430674934.387207 1.000000E 1.000000e+18 1e+18 999999984306749343.872070 10.00000E 1.000000e+19 1e+19 9999999980506448745.727539 100.0000E 1.000000e+20 1e+20 100000002004087710380.554199 1.000000Z 1.000000e+21 1e+21 1000000020040877103805.541992 10.00000Z 1.000000e+22 1e+22 9999999778196308612823.486328 100.0000Z 1.000000e+23 1e+23 99999997781963086128234.863281 1.000000Y 1.000000e+24 1e+24 1000000013848427772521972.656250 10.00000Y 1.000000e+25 1e+25 9999999562023527622222900.390625 100.0000Y 1.000000e+26 1e+26 100000002537764322757720947.265625 1.000000? 1.000000e+27 1e+27 999999988484154701232910156.250000 10.00000? 9.999999e+27 1e+28 9999999442119691371917724609.375000 100.0000? 1.000000e+29 1e+29 100000001504746651649475097656.250000 1.000000? 1.000000e+30 1e+30 1000000015047466516494750976562.500000 10.00000? 1.000000e+31 1e+31 9999999848243210315704345703125.000000 100.0000? 1.000000e+32 1e+32 100000003318135333061218261718750.000000 1.000000? 1.000000e+33 1e+33 999999994495727896690368652343750.000000 10.00000? 1.000000e+34 1e+34 9999999790214771032333374023437500.000000 100.0000? 1.000000e+35 1e+35 100000004091847860813140869140625000.000000 1.000000? 1.000000e+36 1e+36 999999961690316438674926757812500000.000000 10.00000? 1.000000e+37 1e+37 9999999933815813064575195312500000000.000000 100.0000? 1.000000e+38 1e+38 99999996802856898307800292968750000000.000000 340.0001? 3.400000e+38 3.4e+38 339999995214436411857604980468750000000.000000 NaN nan nan nan Inf +inf +inf +inf Complete code with test case.
    #include <msp430.h> #include <stdio.h> #include <stdint.h> #include <string.h> #include <math.h> static void print(char const *s) { while(*s) { while(!(UCA1IFG & UCTXIFG)); UCA1TXBUF = *s++; } } typedef struct { uint32_t s; int e; } TFR; TFR const r[] = { 0x800000UL << 7, 126 + 1, // 0.5 0xCCCCCDUL << 7, 122 + 1, // 0.05 0xA3D70AUL << 7, 119 + 1, // 0.005 0x83126FUL << 7, 116 + 1, // 0.0005 0xD1B717UL << 7, 112 + 1, // 0.00005 0xA7C5ACUL << 7, 109 + 1, // 0.000005 0x8637BDUL << 7, 106 + 1, // 0.0000005 0xD6BF95UL << 7, 102 + 1 // 0.00000005 }; void ftoas(char *a, float const f, unsigned d) { uint32_t s = *(uint32_t *)&f; if (s & (1UL << 31)) *a++ = '-'; int e = (s >> 23) & 0xFF; s <<= 8; s &= ~(1UL << 31); if (e == 255) { if (s) { strcpy(a, "NaN"); } else { strcpy(a, "Inf"); } return; } else if (e == 0) { if (s) { e = 1; while (!(s & (1UL << 31))) s <<= 1, --e; } else { e = 127; } } else { s |= (1UL << 31); } char const * sp = "???????yzafpnum kMGTPEZY????" + 15; if (e < 127) { do { s = s - (s >> 6) - (s >> 7); if (!(s & (1UL << 31))) s <<= 1, --e; e += 10; --sp; } while (e < 127); } else if (e > 135) { while (e > (150 + 16) || (e == (150 + 16) && s > (999999995904ULL >> 16))) { uint64_t n = s; n <<= 32; s = 0; uint64_t d = 1000000000000ULL << (64 - 40); if (n >= d) n -= d, s |= (1UL << 31); d >>= 1; if (n >= d) n -= d, s |= (1UL << 30); d >>= 1; if (n >= d) n -= d, s |= (1UL << 29); d >>= 1; if (n >= d) n -= d, s |= (1UL << 28); d >>= 1; if (n >= d) n -= d, s |= (1UL << 27); d >>= 1; if (n >= d) n -= d, s |= (1UL << 26); d >>= 1; if (n >= d) n -= d, s |= (1UL << 25); d >>= 1; if (n >= d) n -= d, s |= (1UL << 24); d >>= 1; if (n >= d) n -= d, s |= (1UL << 23); d >>= 1; if (n >= d) n -= d, s |= (1UL << 22); d >>= 1; if (n >= d) n -= d, s |= (1UL << 21); d >>= 1; if (n >= d) n -= d, s |= (1UL << 20); d >>= 1; if (n >= d) n -= d, s |= (1UL << 19); d >>= 1; if (n >= d) n -= d, s |= (1UL << 18); d >>= 1; if (n >= d) n -= d, s |= (1UL << 17); d >>= 1; if (n >= d) n -= d, s |= (1UL << 16); d >>= 1; if (n >= d) n -= d, s |= (1UL << 15); d >>= 1; if (n >= d) n -= d, s |= (1UL << 14); d >>= 1; if (n >= d) n -= d, s |= (1UL << 13); d >>= 1; if (n >= d) n -= d, s |= (1UL << 12); d >>= 1; if (n >= d) n -= d, s |= (1UL << 11); d >>= 1; if (n >= d) n -= d, s |= (1UL << 10); d >>= 1; if (n >= d) n -= d, s |= (1UL << 9); d >>= 1; if (n >= d) n -= d, s |= (1UL << 8); //d >>= 1; //if (n >= d) s += (1UL << 8); if (n) s += (1UL << 8); if (!(s & (1UL << 31))) s <<= 1, --e; e -= 39; sp += 4; } while (e > (126 + 10) || (e == (126 + 10) && s >= (1000UL << (32 - 10)))) { uint32_t n = s; s = 0; uint32_t d = 1000UL << (32 - 10); if (n >= d) n -= d, s |= (1UL << 31); d >>= 1; if (n >= d) n -= d, s |= (1UL << 30); d >>= 1; if (n >= d) n -= d, s |= (1UL << 29); d >>= 1; if (n >= d) n -= d, s |= (1UL << 28); d >>= 1; if (n >= d) n -= d, s |= (1UL << 27); d >>= 1; if (n >= d) n -= d, s |= (1UL << 26); d >>= 1; if (n >= d) n -= d, s |= (1UL << 25); d >>= 1; if (n >= d) n -= d, s |= (1UL << 24); d >>= 1; if (n >= d) n -= d, s |= (1UL << 23); d >>= 1; if (n >= d) n -= d, s |= (1UL << 22); d >>= 1; if (n >= d) n -= d, s |= (1UL << 21); d >>= 1; if (n >= d) n -= d, s |= (1UL << 20); d >>= 1; if (n >= d) n -= d, s |= (1UL << 19); d >>= 1; if (n >= d) n -= d, s |= (1UL << 18); d >>= 1; if (n >= d) n -= d, s |= (1UL << 17); d >>= 1; if (n >= d) n -= d, s |= (1UL << 16); d >>= 1; if (n >= d) n -= d, s |= (1UL << 15); d >>= 1; if (n >= d) n -= d, s |= (1UL << 14); d >>= 1; if (n >= d) n -= d, s |= (1UL << 13); d >>= 1; if (n >= d) n -= d, s |= (1UL << 12); d >>= 1; if (n >= d) n -= d, s |= (1UL << 11); d >>= 1; if (n >= d) n -= d, s |= (1UL << 10); d >>= 1; if (n >= d) n -= d, s |= (1UL << 9); d >>= 1; if (n >= d) n -= d, s |= (1UL << 8); d >>= 1; if (n >= d) s += (1UL << 8); if (!(s & (1UL << 31))) s <<= 1, --e; e -= 9; ++sp; } } if (d < 3) d = 3; else if (d > 8) d = 8; if (s) { TFR const *pr = &r[d - 3]; if (e < (126 + 4) || (e == (126 + 4) && s < (10UL << (32 - 4)))) { // < 10 pr += 2; } else if (e < (126 + 7) || (e == (126 + 7) && s < (100UL << (32 - 7)))) { // < 100 ++pr; } s += (pr->s >> (e - pr->e)); if (e == (126 + 10) && s >= (1000UL << (32 - 10))) s = (1UL << 31), e = 127, ++sp; else if (!(s & (1UL << 31))) s >>= 1, s |= (1UL << 31), ++e; } unsigned i = s >> 16; i >>= (136 - e); unsigned id = 1; char c; if (i >= (100 << 6)) { ++id; c = '0'; while (i >= (100 << 6)) i -= (100 << 6), ++c; *a++ = c; } if (id == 2 || i >= (10 << 6)) { ++id; c = '0'; while (i >= (10 << 6)) i -= (10 << 6), ++c; *a++ = c; } c = '0'; while (i >= (1 << 6)) i -= (1 << 6), ++c; *a++ = c; *a++ = '.'; if (e < 130) s >>= (130 - e); else s <<= (e - 130); d -= id; while (d) { s &= ((1UL << 28) - 1); s = (s << 3) + (s << 1); *a++ = '0' + (s >> 28); --d; } *a++ = *sp; *a = 0; } #define smclk_freq (32768UL * 31UL) // SMCLK frequency in hertz #define bps (9600UL) // Async serial bit rate int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer // P4SEL = BIT4 | BIT5; // Enable UART pins P4DIR = BIT4 | BIT5; // // // Initialize UART UCA1CTL1 = UCSWRST; // Hold USCI in reset to allow configuration UCA1CTL0 = 0; // No parity, LSB first, 8 bits, one stop bit, UART (async) const unsigned long brd = (smclk_freq + (bps >> 1)) / bps; // Bit rate divisor UCA1BR1 = (brd >> 12) & 0xFF; // High byte of whole divisor UCA1BR0 = (brd >> 4) & 0xFF; // Low byte of whole divisor UCA1MCTL = ((brd << 4) & 0xF0) | UCOS16; // Fractional divisor, oversampling mode UCA1CTL1 = UCSSEL_2; // Use SMCLK for bit rate generator, release reset char s[32], t[96]; float const tv[] = { 0.0f, 7.1e-46f, 1.0e-45f, 1.0e-44f, 1.0e-43f, 1.0e-42f, 1.0e-41f, 1.0e-40f, 1.0e-39f, 1.2e-38f, 1.0e-37f, 1.0e-36f, 1.0e-35f, 1.0e-34f, 1.0e-33f, 1.0e-32f, 1.0e-31f, 1.0e-30f, 1.0e-29f, 1.0e-28f, 1.0e-27f, 1.0e-26f, 1.0e-25f, 1.0e-24f, 1.0e-23f, 1.0e-22f, 1.0e-21f, 1.0e-20f, 1.0e-19f, 1.0e-18f, 1.0e-17f, 1.0e-16f, 1.0e-15f, 1.0e-14f, 1.0e-13f, 1.0e-12f, 1.0e-11f, 1.0e-10f, 1.0e-9f, 1.0e-8f, 1.0e-7f, 0.000001f, 0.00001f, 0.0001f, 0.001f, 0.01f, 0.1f, 1.0f, 1.23456789f, 10.0f, 100.0f, 1000.0f, 10000.0f, 100000.0f, 1000000.0f, 10000000.0f, 100000000.0f, 1000000000.0f, 1.0e10f, 1.0e11f, 1.0e12f, 1.0e13f, 1.0e14f, 1.0e15f, 1.0e16f, 1.0e17f, 1.0e18f, 1.0e19f, 1.0e20f, 1.0e21f, 1.0e22f, 1.0e23f, 1.0e24f, 1.0e25f, 1.0e26f, 1.0e27f, 1.0e28f, 1.0e29f, 1.0e30f, 1.0e31f, 1.0e32f, 1.0e33f, 1.0e34f, 1.0e35f, 1.0e36f, 1.0e37f, 1.0e38f, 3.4e38f, NAN, INFINITY }; TA0EX0 = 7; TA0CTL = TASSEL_2 | ID_3 | MC_2; TA0CTL |= TACLR; unsigned i; for (i = 0; i < sizeof(tv) / sizeof(tv[0]); ++i) { float const f = tv[i]; ftoas(s, f, 7); //print(s); print("\r\n"); //sprintf(t, "%e", f); //sprintf(t, "%f", f); //sprintf(t, "%g", f); //sprintf(t, "%e %f %g %s\r\n", f, f, f, s); sprintf(t, "%s %e %g %f\r\n", s, f, f, f); print(t); } volatile float et = ((float)TA0R + ((TA0CTL & TAIFG) ? 65536.0 : 0.0)) * 63.0f / 1000000.0f; // Elapsed time in microseconds ftoas(t, et, 5); //sprintf(t, "%f", et); print(t); print("s\r\n"); for(;; return 0; }
  21. Like
    oPossum got a reaction from agaelema in Printing large numbers in small spaces (64 bits in 5 chars)   
    This code will print a 64 bit unsigned integer in 5 characters using 3 significant digits and and an SI prefix. Several revisions are shown beginning with the obvious implementation and then optimizing it to improve speed and decrease code size. Compilers used are TI 4.4.4 and GCC 4.9.1 Test hardware is the F5529 Launchpad running at the default clock of 1.016 MHz.
     
    The general strategy of the code is:
    - Count the number of digits in the decimal representation
    - Divide the value to reduce it to 3 digits.
    - Print the 3 digits with proper formatting and SI prefix.
    - Handle the special case of values of 99 or less that must be printed with 2 or 1 digits and no decimal.
     
    A series of values from zero to the maximum possible 64 bit value are used to test the performance and correct operation of the code...

    static const uint64_t td[] = { 0ULL, 1ULL, 12ULL, 123ULL, 1234ULL, 12345ULL, 123456ULL, 1234567ULL, 12345678ULL, 123456789ULL, 1234567890ULL, 12345678901ULL, 123456789012ULL, 1234567890123ULL, 12345678901234ULL, 123456789012345ULL, 1234567890123456ULL, 12345678901234567ULL, 123456789012345678ULL, 1234567890123456789ULL, 12345678901234567890ULL, 18446744073709551615ULL }; The first version uses C standard library functions for a very simple implementation. Decimal digits are counted using log10(). Dividing the digit count by three using the div() function provides values for formatting, division, and the SI prefix. The divisor needed to reduce the value to three digits is calculated with pow(). The special case of values of 99 or less is handled by using a fixed digit count for values less than 1000 rather than the actual base ten digit count. Conversion from uint64_t to double will have a loss of precision for large values due to float having a 52 bit significand. This is not a problem for this code because only three significant digits are printed.Code size for the TI compiler is 20,636 bytes and the execution time for the test case is 1.22 seconds. The GCC compiler does not produce working code.

    static void sprint_f3d(char * s, double f) { static char const * const fmt[3] = { "%1.2f%c", "%2.1f%c", "%4.0f%c" }; div_t const d = div((f < 1000.0) ? 2 : (int)log10(f), 3); sprintf(s, fmt[d.rem], f / pow(1000.0, d.quot), " kMGTPEZY"[d.quot]); } The C standard library does not have integer versions of log10() and pow(), so another strategy will be used. The value is divided by 10 until it is less than 1000. The number of divisions is counted to determine the number of base ten digits. Stopping the division when the value is less than 1000 handles the special case for values of 99 or less by limiting the digit count to 3 or more. The resulting 3 digit value will be split as necessary for formatting using the div() library function.Code size for the TI compiler is 5,346 bytes and the execution time for the test case is 3.24 seconds.
    Code size for the GCC compiler is 36,040 bytes and the execution time for the test case is 981 milliseconds.
    The poor performance of the TI compiler is due the an inefficient intrinsic 64 bit unsigned divide.

    static void sprint_u64_a(char *s, uint64_t n) { unsigned d = 2; while(n >= 1000) n /= 10, ++d; div_t qr = div(d, 3); char const u = " kMGTPE"[qr.quot]; switch(qr.rem) { case 0: qr = div((int)n, 100); sprintf(s, "%i.%02i%c", qr.quot, qr.rem, u); break; case 1: qr = div((int)n, 10); sprintf(s, "%2i.%i%c", qr.quot, qr.rem, u); break; case 2: sprintf(s, "%4i%c", (int)n, u); break; } } Using this 64 bit unsigned divide provides much better performance on the TI compiler, but much worse on the GCC compiler.Code size for the TI compiler is 5,666 bytes and the execution time for the test case is 396 milliseconds.
    Code size for the GCC compiler is 36,116 bytes and the execution time for the test case is 4.06 seconds.
    Future revisions will use intrinsic divide for GCC and this divide function for TI.

    static uint64_t divu64(uint64_t n, uint64_t d) { if((n < d) || (!d)) return 0; uint64_t register b = 1; if(((uint16_t)(n >> 48)) & 0x8000) { while(!(((uint16_t)(d >> 48)) & 0x8000)) d <<= 1, b <<= 1; if(n < d) d >>= 1, b >>= 1; } else { d <<= 1; while(n >= d) d <<= 1, b <<= 1; d >>= 1; } uint64_t q = b; n -= d; while(!(b & 1)) { d >>= 1; b >>= 1; if(n >= d) n -= d, q |= b; } return q; } The number of 64 bit divides can be reduced by using constants of 10 to the power of 2 to the power of N rather than iterative division by 10.Code size for the TI compiler is 5,886 bytes and the execution time for the test case is 160 milliseconds.
    Code size for the GCC compiler is 36,256 bytes and the execution time for the test case is 223 milliseconds.

    unsigned d = 2; if(n >= 1000000000000000000ULL) n = divu64(n, 10000000000000000ULL), d += 16; if(n >= 10000000000ULL) n = divu64(n, 100000000ULL), d += 8; if(n >= 1000000ULL) n = divu64(n, 10000ULL), d += 4; if(n >= 10000ULL) n = divu64(n, 100ULL), d += 2; if(n >= 1000ULL) n = divu64(n, 10ULL), ++d; The number of 64 bit divides can be reduced to one by using a table to determine the number of base ten digits. The table is searched for the highest value that is less than or equal to the value to be printed. The value is then divided by the table entry that is 1/100th of the value representing the base ten digit count to reduce the value to 3 digits.Code size for the TI compiler is 5,886 bytes and the execution time for the test case is 143 milliseconds.
    Code size for the GCC compiler is 36,096 bytes and the execution time for the test case is 143 milliseconds.

    static uint64_t const pt[] = { // Powers of 10 // 18446744073709551615 // 2^64 - 1 10000000000000000000ULL, // 10^19 NN.N E 1000000000000000000ULL, // 10^18 N.NN E 100000000000000000ULL, // 10^17 NNN P 10000000000000000ULL, // 10^16 NN.N P 1000000000000000ULL, // 10^15 N.NN P 100000000000000ULL, // 10^14 NNN T 10000000000000ULL, // 10^13 NN.N T 1000000000000ULL, // 10^12 N.NN T 100000000000ULL, // 10^11 NNN G 10000000000ULL, // 10^10 NN.N G // 4294967295 // 2^32 - 1 1000000000ULL, // 10^9 N.NN G 100000000ULL, // 10^8 NNN M 10000000ULL, // 10^7 NN.N M 1000000ULL, // 10^6 N.NN M 100000ULL, // 10^5 NNN k 10000ULL, // 10^4 NN.N k 1000ULL, // 10^3 N.NN k 100ULL, // 10^2 NNN 10ULL, // 10^1 NN 1ULL, // 10^0 N }; unsigned d = 2; if(n >= 1000) { d = 19; uint64_t const *p = pt; while (n < *p) ++p, --d; n = divu64(n, p[2]); } The sprintf() function takes significant space, so it is replaced with decimal to ASCII conversion using the div() library function.Code size for the TI compiler is 1,660 bytes and the execution time for the test case is 25.8 milliseconds.
    Code size for the GCC compiler is 3,384 bytes and the execution time for the test case is 82.0 milliseconds.

    div_t qr; qr.rem = (int)n; div_t const dp = div(d, 3); if(dp.rem == 2) *s++ = ' '; if(qr.rem < 100) { *s++ = ' '; qr.quot = 0; } else { qr = div(qr.rem, 100); *s++ = '0' + qr.quot; } if(dp.rem == 0) *s++ = '.'; if((!qr.quot) && (qr.rem < 10)) { *s++ = ' '; } else { qr = div(qr.rem, 10); *s++ = '0' + qr.quot; } if(dp.rem == 1) *s++ = '.'; *s++ = '0' + qr.rem; *s++ = " kMGTPE"[dp.quot]; *s = 0; The final optimization removes all division. The base ten tables values are used with iterative subtraction to create the ASCII string.Code size for the TI compiler is 1,368 bytes and the execution time for the test case is 10.8 milliseconds.
    Code size for the GCC compiler is 2,452 bytes and the execution time for the test case is 19.9 milliseconds.

    uint64_t const *p = pt + 17; if(n >= 1000) { p = pt; while (n < *p) ++p; } // n += (p[2] >> 1); // optional rounding int dp = p - pt; while (dp > 2) dp -= 3; if (dp == 2) *s++ = ' '; char c; if(n < 100) { c = ' '; } else { c = '0'; while(n >= *p) n -= *p, ++c; } *s = c; ++p; if(dp == 1) *++s = '.'; if((*s == ' ') && (n < 10)) { c = ' '; } else { c = '0'; while(n >= *p) n -= *p, ++c; } *++s = c; ++p; if(dp == 0) *++s = '.'; c = '0'; while (n >= *p) n -= *p, ++c; *++s = c; *++s = " EEPPPTTTGGGMMMkkk "[p - pt]; *++s = 0; Performance summary
    function TI GCC ---------------------------------------------- sprint_u64 1368 10.8 ms 2452 19.9 ms sprint_u64_e 1660 25.8 ms 3384 82.0 ms sprint_u64_d 5886 143 ms 36096 143 ms sprint_u64_c 5886 160 ms 36256 223 ms sprint_u64_b 5666 396 ms 36116 4.06 s sprint_u64_a 5346 3.24 s 36040 981 ms sprint_f3d 20636 1.22 s non-functional Network bandwidth display (lower right) using 3 digits.
     
    Complete code

    #include <msp430.h> #include <stdint.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <stdio.h> #define PU64 sprint_u64 //#define PU64 sprint_u64_e //#define PU64 sprint_u64_d //#define PU64 sprint_u64_c //#define PU64 sprint_u64_b //#define USE_DIV_FUNC //#define PU64 sprint_u64_a //#define PU64 sprint_f3d //#define PU64 sprint_u64_null static void sprint_u64(char *s, uint64_t n) { static uint64_t const pt[] = { // Powers of 10 // 18446744073709551615 // 2^64 - 1 10000000000000000000ULL, // 10^19 NN.N E 1000000000000000000ULL, // 10^18 N.NN E 100000000000000000ULL, // 10^17 NNN P 10000000000000000ULL, // 10^16 NN.N P 1000000000000000ULL, // 10^15 N.NN P 100000000000000ULL, // 10^14 NNN T 10000000000000ULL, // 10^13 NN.N T 1000000000000ULL, // 10^12 N.NN T 100000000000ULL, // 10^11 NNN G 10000000000ULL, // 10^10 NN.N G // 4294967295 // 2^32 - 1 1000000000ULL, // 10^9 N.NN G 100000000ULL, // 10^8 NNN M 10000000ULL, // 10^7 NN.N M 1000000ULL, // 10^6 N.NN M 100000ULL, // 10^5 NNN k 10000ULL, // 10^4 NN.N k 1000ULL, // 10^3 N.NN k 100ULL, // 10^2 NNN 10ULL, // 10^1 NN 1ULL, // 10^0 N }; uint64_t const *p = pt + 17; if(n >= 1000) { p = pt; while (n < *p) ++p; } // n += (p[2] >> 1); // optional rounding int dp = p - pt; while (dp > 2) dp -= 3; if (dp == 2) *s++ = ' '; char c; if(n < 100) { c = ' '; } else { c = '0'; while(n >= *p) n -= *p, ++c; } *s = c; ++p; if(dp == 1) *++s = '.'; if((*s == ' ') && (n < 10)) { c = ' '; } else { c = '0'; while(n >= *p) n -= *p, ++c; } *++s = c; ++p; if(dp == 0) *++s = '.'; c = '0'; while (n >= *p) n -= *p, ++c; *++s = c; *++s = " EEPPPTTTGGGMMMkkk "[p - pt]; *++s = 0; } static uint64_t divu64(uint64_t n, uint64_t d) { #if defined( __GNUC__) & !defined(USE_DIV_FUNC) return n / d; #else if((n < d) || (!d)) return 0; uint64_t register b = 1; if(((uint16_t)(n >> 48)) & 0x8000) { while(!(((uint16_t)(d >> 48)) & 0x8000)) d <<= 1, b <<= 1; if(n < d) d >>= 1, b >>= 1; } else { d <<= 1; while(n >= d) d <<= 1, b <<= 1; d >>= 1; } uint64_t q = b; n -= d; while(!(b & 1)) { d >>= 1; b >>= 1; if(n >= d) n -= d, q |= b; } return q; #endif } static void sprint_u64_e(char *s, uint64_t n) { static uint64_t const pt[] = { // Powers of 10 // 18446744073709551615 // 2^64 - 1 10000000000000000000ULL, // 10^19 NN.N E 1000000000000000000ULL, // 10^18 N.NN E 100000000000000000ULL, // 10^17 NNN P 10000000000000000ULL, // 10^16 NN.N P 1000000000000000ULL, // 10^15 N.NN P 100000000000000ULL, // 10^14 NNN T 10000000000000ULL, // 10^13 NN.N T 1000000000000ULL, // 10^12 N.NN T 100000000000ULL, // 10^11 NNN G 10000000000ULL, // 10^10 NN.N G // 4294967295 // 2^32 - 1 1000000000ULL, // 10^9 N.NN G 100000000ULL, // 10^8 NNN M 10000000ULL, // 10^7 NN.N M 1000000ULL, // 10^6 N.NN M 100000ULL, // 10^5 NNN k 10000ULL, // 10^4 NN.N k 1000ULL, // 10^3 N.NN k 100ULL, // 10^2 NNN 10ULL, // 10^1 NN 1ULL, // 10^0 N }; unsigned d = 2; if(n >= 1000) { d = 19; uint64_t const *p = pt; while (n < *p) ++p, --d; n = divu64(n, p[2]); } div_t qr; qr.rem = (int)n; div_t const dp = div(d, 3); if(dp.rem == 2) *s++ = ' '; if(qr.rem < 100) { *s++ = ' '; qr.quot = 0; } else { qr = div(qr.rem, 100); *s++ = '0' + qr.quot; } if(dp.rem == 0) *s++ = '.'; if((!qr.quot) && (qr.rem < 10)) { *s++ = ' '; } else { qr = div(qr.rem, 10); *s++ = '0' + qr.quot; } if(dp.rem == 1) *s++ = '.'; *s++ = '0' + qr.rem; *s++ = " kMGTPE"[dp.quot]; *s = 0; } static void sprint_u64_d(char *s, uint64_t n) { static uint64_t const pt[] = { // Powers of 10 // 18446744073709551615 // 2^64 - 1 10000000000000000000ULL, // 10^19 NN.N E 1000000000000000000ULL, // 10^18 N.NN E 100000000000000000ULL, // 10^17 NNN P 10000000000000000ULL, // 10^16 NN.N P 1000000000000000ULL, // 10^15 N.NN P 100000000000000ULL, // 10^14 NNN T 10000000000000ULL, // 10^13 NN.N T 1000000000000ULL, // 10^12 N.NN T 100000000000ULL, // 10^11 NNN G 10000000000ULL, // 10^10 NN.N G // 4294967295 // 2^32 - 1 1000000000ULL, // 10^9 N.NN G 100000000ULL, // 10^8 NNN M 10000000ULL, // 10^7 NN.N M 1000000ULL, // 10^6 N.NN M 100000ULL, // 10^5 NNN k 10000ULL, // 10^4 NN.N k 1000ULL, // 10^3 N.NN k 100ULL, // 10^2 NNN 10ULL, // 10^1 NN 1ULL, // 10^0 N }; unsigned d = 2; if(n >= 1000) { d = 19; uint64_t const *p = pt; while (n < *p) ++p, --d; n = divu64(n, p[2]); } div_t qr = div(d, 3); char const u = " kMGTPE"[qr.quot]; switch(qr.rem) { case 0: qr = div((int)n, 100); sprintf(s, "%i.%02i%c", qr.quot, qr.rem, u); break; case 1: qr = div((int)n, 10); sprintf(s, "%2i.%i%c", qr.quot, qr.rem, u); break; case 2: sprintf(s, "%4i%c", (int)n, u); break; } } static void sprint_u64_c(char *s, uint64_t n) { unsigned d = 2; if(n >= 1000000000000000000ULL) n = divu64(n, 10000000000000000ULL), d += 16; if(n >= 10000000000ULL) n = divu64(n, 100000000ULL), d += 8; if(n >= 1000000ULL) n = divu64(n, 10000ULL), d += 4; if(n >= 10000ULL) n = divu64(n, 100ULL), d += 2; if(n >= 1000ULL) n = divu64(n, 10ULL), ++d; div_t qr = div(d, 3); char const u = " kMGTPE"[qr.quot]; switch(qr.rem) { case 0: qr = div((int)n, 100); sprintf(s, "%i.%02i%c", qr.quot, qr.rem, u); break; case 1: qr = div((int)n, 10); sprintf(s, "%2i.%i%c", qr.quot, qr.rem, u); break; case 2: sprintf(s, "%4i%c", (int)n, u); break; } } static void sprint_u64_b(char *s, uint64_t n) { unsigned d = 2; while(n >= 1000) n = divu64(n, 10), ++d; div_t qr = div(d, 3); char const u = " kMGTPE"[qr.quot]; switch(qr.rem) { case 0: qr = div((int)n, 100); sprintf(s, "%i.%02i%c", qr.quot, qr.rem, u); break; case 1: qr = div((int)n, 10); sprintf(s, "%2i.%i%c", qr.quot, qr.rem, u); break; case 2: sprintf(s, "%4i%c", (int)n, u); break; } } static void sprint_u64_a(char *s, uint64_t n) { unsigned d = 2; while(n >= 1000) n /= 10, ++d; div_t qr = div(d, 3); char const u = " kMGTPE"[qr.quot]; switch(qr.rem) { case 0: qr = div((int)n, 100); sprintf(s, "%i.%02i%c", qr.quot, qr.rem, u); break; case 1: qr = div((int)n, 10); sprintf(s, "%2i.%i%c", qr.quot, qr.rem, u); break; case 2: sprintf(s, "%4i%c", (int)n, u); break; } } static void sprint_f3d(char * s, double f) { static char const * const fmt[3] = { "%1.2f%c", "%2.1f%c", "%4.0f%c" }; div_t const d = div((f < 1000.0) ? 2 : (int)log10(f), 3); sprintf(s, fmt[d.rem], f / pow(1000.0, d.quot), " kMGTPEZY"[d.quot]); } static void sprint_u64_null(char *s, uint64_t n) { *s = 0; } static void print(char const *s) { while(*s) { while(!(UCA1IFG & UCTXIFG)); UCA1TXBUF = *s++; } } #define smclk_freq (32768UL * 31UL) // SMCLK frequency in hertz #define bps (9600UL) // Async serial bit rate int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer // P4SEL = BIT4 | BIT5; // Enable UART pins P4DIR = BIT4 | BIT5; // // // Initialize UART UCA1CTL1 = UCSWRST; // Hold USCI in reset to allow configuration UCA1CTL0 = 0; // No parity, LSB first, 8 bits, one stop bit, UART (async) const unsigned long brd = (smclk_freq + (bps >> 1)) / bps; // Bit rate divisor UCA1BR1 = (brd >> 12) & 0xFF; // High byte of whole divisor UCA1BR0 = (brd >> 4) & 0xFF; // Low byte of whole divisor UCA1MCTL = ((brd << 4) & 0xF0) | UCOS16; // Fractional divisor, oversampling mode UCA1CTL1 = UCSSEL_2; // Use SMCLK for bit rate generator, release reset static const uint64_t td[] = { 0ULL, 1ULL, 12ULL, 123ULL, 1234ULL, 12345ULL, 123456ULL, 1234567ULL, 12345678ULL, 123456789ULL, 1234567890ULL, 12345678901ULL, 123456789012ULL, 1234567890123ULL, 12345678901234ULL, 123456789012345ULL, 1234567890123456ULL, 12345678901234567ULL, 123456789012345678ULL, 1234567890123456789ULL, 12345678901234567890ULL, 18446744073709551615ULL }; char s[32]; int n; uint64_t const *p; // Print test array to application UART print("\r\n"); n = sizeof(td) / sizeof(td[0]); p = td; do { PU64(s, *p++); print(s); print("\r\n"); } while(--n); // Time the test array - make string only - do not print TA0EX0 = 7; TA0CTL = TASSEL_2 | ID_3 | MC_2; TA0CTL |= TACLR; n = sizeof(td) / sizeof(td[0]); p = td; do { PU64(s, *p++); } while(--n); uint64_t et = TA0R * 63ULL; // Elapsed time in microseconds PU64(s, et); print(s); print(" us\r\n"); for(;; return 0; }
  22. Like
    oPossum got a reaction from 0xFFFF in Interfacing with DHT11 Humidty + Temp sensor   
    This code will display the DHT11 hex data, and humidity and temperature in decimal.
    It uses software UART so it will work on all versions of the Launchpad.
    DHT11 is connect to P1.4



     
    #include <msp430.h> #include <stdlib.h> volatile unsigned txdata = 0; #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer0_A0(void) { if(txdata) { (txdata & 1) ? (CCTL0 &= ~OUTMOD2) : (CCTL0 |= OUTMOD2); txdata >>= 1; CCR0 += 104; } else { CCTL0 &= ~CCIE; } } void putc(const char c) { while(CCTL0 & CCIE); txdata = 0x0200 | (c << 1); CCR0 = TAR + 16; CCTL0 = OUTMOD0 | CCIE; } void print(const char *s) { while(*s) putc(*s++); } void print_hex_digit(unsigned n) { putc("0123456789ABCDEF"[n & 0x0F]); } void print_hex_byte(unsigned n) { print_hex_digit(n >> 4); print_hex_digit(n); } void print_dec2(int n) { // Print 2 decimal digits 0 to 99 const div_t d = div(n, 10); if(d.quot) putc('0' + d.quot); putc('0' + d.rem); } int read_dht(unsigned char *p) { // Note: TimerA must be continuous mode (MC_2) at 1 MHz const unsigned b = BIT4; // I/O bit const unsigned char *end = p + 6; // End of data buffer register unsigned char m = 1; // First byte will have only start bit register unsigned st, et; // Start and end times // p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = 0; // Clear data buffer // P1OUT &= ~b; // Pull low P1DIR |= b; // Output P1REN &= ~b; // Drive low st = TAR; while((TAR - st) < 18000); // Wait 18 ms P1REN |= b; // Pull low P1OUT |= b; // Pull high P1DIR &= ~b; // Input // st = TAR; // Get start time for timeout while(P1IN & if((TAR - st) > 100) return -1; // Wait while high, return if no response et = TAR; // Get start time for timeout do { // st = et; // Start time of this bit is end time of previous bit while(!(P1IN & ) if((TAR - st) > 100) return -2; // Wait while low, return if stuck low while(P1IN & if((TAR - st) > 200) return -3; // Wait while high, return if stuck high et = TAR; // Get end time if((et - st) > 110) *p |= m; // If time > 110 us, then it is a one bit if(!(m >>= 1)) m = 0x80, ++p; // Shift mask, move to next byte when mask is zero } while(p < end); // Do until array is full // p -= 6; // Point to start of buffer if(p[0] != 1) return -4; // No start bit if(((p[1] + p[2] + p[3] + p[4]) & 0xFF) != p[5]) return -5; // Bad checksum // return 0; // Good read } void main(void) { unsigned char dht[6]; // Data from DHT11 int err; // unsigned n; // // WDTCTL = WDTPW | WDTHOLD; // Disable watchdog reset DCOCTL = 0; // Run at 1 MHz BCSCTL1 = CALBC1_1MHZ; // DCOCTL = CALDCO_1MHZ; // CCTL0 = OUT; // Setup serial tx I/O P1SEL = BIT1; // P1DIR = BIT1; // TACTL = TASSEL_2 | MC_2; // TimerA SMCLK, continuous __enable_interrupt(); // // for(; { // for-ever while(CCTL0 & CCIE); // Wait for any serial tx to finish err = read_dht(dht); // Read DHT11 if(err) { // If error... print("Error -"); // putc('0' - err); // print("\r\n"); // } else { // No error... for(n = 0; n < sizeof(dht); ++n) { // Show hex data print_hex_byte(dht[n]); // putc(' '); // } // print(" "); // print_dec2(dht[1]); // Show humimidy print(" RH "); // print_dec2(dht[3]); // Show temperature print(" C\r\n"); // } // __delay_cycles(1000000); // Wait a while } // }  
     
  23. Like
    oPossum got a reaction from tripwire in Game of Life with G2211   
    // main.c #include "msp430g2211.h" #include "string.h" void video_init(void); unsigned pix[14], nxt[14]; unsigned get_pix(int x, int y) { if(x < 0 || x > 15 || y < 0 || y > 13) return 0; return (pix[y] >> x) & 1; } void main(void) { int x, y, n; WDTCTL = WDTPW + WDTHOLD; // No watchdog BCSCTL1 = XT2OFF + 15; // DCO 16 Mhz DCOCTL = 0x86; // // P1DIR = 0xF2; // I/O assignment P1REN = 0x0D; // Pull up resistor P1OUT = 0x0F; // P1SEL = 0x50; // Enable Timer A output, SMCLK output video_init(); // Begin video generation // Initial GOL field pix[0] = pix[2] = pix[4] = pix[6] = pix[8] = pix[10] = pix[12] = 0x5555; pix[1] = pix[3] = pix[5] = pix[7] = pix[9] = pix[11] = pix[13] = 0xAAAA; pix[0] = 0x0000; pix[1] = 0x000E; pix[2] = 0x0000; pix[3] = 0; memcpy(nxt, pix, sizeof(nxt)); for(; { for(y = 0; y < 14; ++y) { for(x = 0; x < 16; ++x) { n = get_pix(x - 1, y - 1) + get_pix(x , y - 1) + get_pix(x + 1, y - 1) + get_pix(x - 1, y ) + /* me */ get_pix(x + 1, y ) + get_pix(x - 1, y + 1) + get_pix(x , y + 1) + get_pix(x + 1, y + 1); if(get_pix(x, y)) { if(n < 2 || n > 3) nxt[y] &= ~(1 << x); } else { if(n == 3) nxt[y] |= (1 << x); } } } __delay_cycles(5000000); memcpy(pix, nxt, sizeof(pix)); } }

    ; video.asm .cdecls C, LIST, "msp430g2211.h" .text .global video_init .global pix .bss sync_state, 2 .bss sync_lines, 2 .bss video_state, 2 .bss vid_line, 2 video_init ; --- Initialize Video Generation --- clr &sync_state ; Init state machine mov #1, sync_lines ; clr &video_state ; mov #1016, &TACCR0 ; Setup Timer A period for 63.5 us mov #75, &TACCR1 ; Setup Timer A compare for video sync mov #0x0210, &TACTL ; Timer A config: SMCLK, count up mov #0x0060, &TACCTL1 ; Setup Timer A set/reset output mode bis #0x0010, &TACCTL0 ; Enable PWM interupt eint ; Enable interupts ret ; ; video_isr ; --- Video ISR --- dec &sync_lines ; Decrement sync lines jne vid_state ; Skip sync if not zero... add &sync_state, PC ; Jump to sync handler... ; - Field 1/3 jmp vid_eq ; Pre Equalizing jmp vid_vsync ; Vertical Sync jmp vid_eq ; Post Equalizing jmp vid_vbi ; VBI jmp vid_blank_9 ; 9 blank lines jmp vid_active ; Active Video jmp vid_blank_9 ; 9 blank lines jmp vid_half ; Video half line ; - Field 2/4 jmp vid_eq ; Pre Equalizing jmp vid_vsync ; Vertical Sync jmp vid_eq ; Post Equalizing jmp vid_half ; Video half line jmp vid_vbi ; VBI jmp vid_blank_9 ; 9 blank lines jmp vid_active ; Active Video jmp vid_blank_9l ; 9 blank lines vid_state ; -- Active Video Generation add &video_state, PC ; reti ; No active video jmp vid_field ; Field ; vid_eq ; Equalizing pulses - 6 half lines mov #508, &TACCR0 ; Half line mov #38, &TACCR1 ; Equalizing pulse duration mov #6, &sync_lines ; 6 half lines clr &video_state ; No video generation during sync incd &sync_state ; Next state reti ; ; vid_vsync ; Vertical sync - 6 half lines mov #508 - 75, &TACCR1 ; Sync pulse duration mov #6, &sync_lines ; 6 half lines incd &sync_state ; Next state reti ; ; vid_half ; Video half line - 1 half line mov #508, &TACCR0 ; Half line mov #75, &TACCR1 ; Sync pulse duration mov #1, &sync_lines ; 1 half line incd &sync_state ; Next state clr &video_state ; No video generation during half line and sync reti ; ; vid_vbi ; Vertical blanking interval - 11 lines mov #1016, &TACCR0 ; Full line mov #75, &TACCR1 ; Sync pulse duration mov #11, &sync_lines ; 11 lines incd &sync_state ; Next state reti ; ; vid_active ; Active video - 224 lines mov #224, &sync_lines ; 224 lines incd &sync_state ; Next state mov #2, &video_state ; Genearte video clr &vid_line ; Clear video line count jmp vid_field ; Generate video... ; vid_blank_9 ; 9 blank lines mov #9, &sync_lines ; incd &sync_state ; clr &video_state ; reti ; ; vid_blank_9l ; 9 blank lines (last in field) mov #9, &sync_lines ; clr &sync_state ; clr &video_state ; reti ; ; vid_field ; push R10 ; push R12 ; push R14 ; ; mov &TAR, R10 ; cmp #176, R10 ; jlo $ - 8 ; and #7, R10 ; rla R10 ; add R10, PC ; nop ; nop ; nop ; nop ; nop ; nop ; nop ; ; mov &vid_line, R12 ; inc &vid_line ; rra R12 ; rra R12 ; rra R12 ; ;bic #1, R12 ; add #pix, R12 ; mov @R12, R12 ; mov #16, R14 ; pixloop rlc R12 ; jc pixon ; bic.b #0x80, &P1OUT ; jmp nexpix ; pixon bis.b #0x80, &P1OUT ; jmp nexpix ; nexpix mov #10, R10 ; dec R10 ; jne $ - 2 ; dec R14 ; jne pixloop ; nop ; nop ; nop ; bic.b #0x80, &P1OUT ; ; pop R14 ; pop R12 ; pop R10 ; ; reti ; ; ; Interrupt Vectors .sect ".int09" ; TACCR0 CCIFG .short video_isr ; ; .end ;

    I can explain the video code if anyone is interested. It is very flexible and does proper interlaced video and sync (equalization and serration).
  24. Like
    oPossum got a reaction from tripwire in Fraunchpad Synth - Still Alive   
    The note envelope is a simple exponential decay. After each cycle of the waveform, the level is multiplied by 65408/65536 (0.99805). Higher frequency notes will decay faster then lower frequency.

    The music data is in an array in FRAM, it is not MIDI driven like the previous version

    Inspired by this project

    main.c
    #include "msp430fr5739.h" #include "alive.h" void set_tick(unsigned); unsigned get_tick(void); void synth_init(void); void set_note(int, int, int); int wave[257] = { 0, 804, 1608, 2410, 3212, 4011, 4808, 5602, 6393, 7179, 7962, 8739, 9512, 10278, 11039, 11793, 12539, 13279, 14010, 14732, 15446, 16151, 16846, 17530, 18204, 18868, 19519, 20159, 20787, 21403, 22005, 22594, 23170, 23731, 24279, 24811, 25329, 25832, 26319, 26790, 27245, 27683, 28105, 28510, 28898, 29268, 29621, 29956, 30273, 30571, 30852, 31113, 31356, 31580, 31785, 31971, 32137, 32285, 32412, 32521, 32609, 32678, 32728, 32757, 32767, 32757, 32728, 32678, 32609, 32521, 32412, 32285, 32137, 31971, 31785, 31580, 31356, 31113, 30852, 30571, 30273, 29956, 29621, 29268, 28898, 28510, 28105, 27683, 27245, 26790, 26319, 25832, 25329, 24811, 24279, 23731, 23170, 22594, 22005, 21403, 20787, 20159, 19519, 18868, 18204, 17530, 16846, 16151, 15446, 14732, 14010, 13279, 12539, 11793, 11039, 10278, 9512, 8739, 7962, 7179, 6393, 5602, 4808, 4011, 3212, 2410, 1608, 804, 0, -804, -1608, -2410, -3212, -4011, -4808, -5602, -6393, -7179, -7962, -8739, -9512, -10278, -11039, -11793, -12539, -13279, -14010, -14732, -15446, -16151, -16846, -17530, -18204, -18868, -19519, -20159, -20787, -21403, -22005, -22594, -23170, -23731, -24279, -24811, -25329, -25832, -26319, -26790, -27245, -27683, -28105, -28510, -28898, -29268, -29621, -29956, -30273, -30571, -30852, -31113, -31356, -31580, -31785, -31971, -32137, -32285, -32412, -32521, -32609, -32678, -32728, -32757, -32767, -32757, -32728, -32678, -32609, -32521, -32412, -32285, -32137, -31971, -31785, -31580, -31356, -31113, -30852, -30571, -30273, -29956, -29621, -29268, -28898, -28510, -28105, -27683, -27245, -26790, -26319, -25832, -25329, -24811, -24279, -23731, -23170, -22594, -22005, -21403, -20787, -20159, -19519, -18868, -18204, -17530, -16846, -16151, -15446, -14732, -14010, -13279, -12539, -11793, -11039, -10278, -9512, -8739, -7962, -7179, -6393, -5602, -4808, -4011, -3212, -2410, -1608, -804, 0 }; void main(void) { unsigned osc; const struct event_t *e; WDTCTL = WDTPW + WDTHOLD; CSCTL0 = 0xA500; // Unlock clock registers CSCTL1 = DCORSEL | DCOFSEL1 | DCOFSEL0; // 24 MHz CSCTL2 = 0x0333; // Use DCO clock for ACLK, SMCLK and MCLK CSCTL3 = 0x0000; // Set all clock divider to 1 //CSCTL4 = //CSCTL5 = P1DIR = 0x01; // PWM audio output on P1.0 P1REN = 0x00; // P1OUT = 0x00; // P1SEL0 = 0x01; // Enable Timer A output P1SEL1 = 0x00; // P2DIR = 0x00; // P2REN = 0x00; // P2OUT = 0x00; // P2SEL0 = 0x00; // P2SEL1 = 0x03; // Enable UART UCA0 P3DIR = 0x10; // P3REN = 0x00; // P3OUT = 0x00; // P3SEL0 = 0x10; // SMCLK output P3SEL1 = 0x10; // SMCLK output P4DIR = 0x00; // P4REN = 0x00; // P4OUT = 0x00; // P4SEL0 = 0x00; // P4SEL1 = 0x00; // PJDIR = 0x0F; // 4 LEDs PJREN = 0x00; // PJOUT = 0x00; // PJSEL0 = 0x00; // PJSEL1 = 0x00; // UCA0MCTLW = 1; // 16x oversampling UCA0BRW = 24000000 / 16 / 9600; // Fraunchpad UART //UCA0BRW = 24000000 / 16/ 31250; // Standard MIDI bit rate UCA0CTLW0 = 0x0080; // Use SMCLK for bit rate generator, release reset synth_init(); for(osc = 0, e = tune_still_alive; e->pitch; ++e) { set_note(osc++ & 7, e->pitch, 9000); ++PJOUT; set_tick((e[1].time - e[0].time) * 150); while(get_tick()); } }

    alive.h

    struct event_t { unsigned time; unsigned pitch; }; static const struct event_t tune_still_alive[] = { 192, 91, 240, 90, 288, 88, 336, 88, 384, 69, 384, 90, 432, 74, 480, 78, 528, 74, 576, 71, 624, 74, 672, 78, 720, 74, 768, 69, 816, 74, 864, 78, 912, 74, 912, 81, 960, 71, 960, 91, 1008, 74, 1008, 90, 1056, 78, 1056, 88, 1104, 74, 1104, 88, 1152, 69, 1200, 74, 1200, 90, 1248, 78, 1296, 74, 1344, 71, 1344, 86, 1392, 74, 1440, 78, 1440, 88, 1488, 74, 1488, 81, 1536, 69, 1584, 74, 1632, 78, 1680, 74, 1728, 71, 1776, 74, 1824, 78, 1872, 74, 1872, 81, 1920, 71, 1920, 88, 1968, 76, 2016, 79, 2016, 90, 2064, 76, 2064, 91, 2112, 71, 2160, 76, 2208, 79, 2208, 88, 2256, 76, 2256, 85, 2304, 69, 2352, 73, 2352, 86, 2400, 79, 2448, 73, 2496, 69, 2496, 88, 2544, 73, 2592, 79, 2592, 81, 2640, 73, 2640, 81, 2688, 69, 2736, 74, 2736, 90, 2784, 78, 2832, 74, 2880, 71, 2928, 74, 2976, 78, 3024, 74, 3072, 69, 3120, 74, 3168, 78, 3216, 74, 3264, 71, 3264, 91, 3312, 74, 3312, 90, 3360, 78, 3360, 88, 3408, 74, 3408, 88, 3456, 69, 3456, 90, 3504, 74, 3552, 78, 3600, 74, 3648, 71, 3696, 74, 3744, 78, 3792, 74, 3840, 69, 3888, 74, 3936, 78, 3984, 74, 3984, 81, 4032, 71, 4032, 91, 4080, 74, 4080, 90, 4128, 78, 4128, 88, 4176, 74, 4176, 88, 4224, 69, 4272, 74, 4320, 78, 4320, 90, 4368, 74, 4368, 86, 4416, 71, 4464, 74, 4512, 78, 4512, 88, 4560, 74, 4560, 81, 4608, 69, 4656, 74, 4704, 78, 4752, 74, 4800, 71, 4848, 74, 4896, 78, 4944, 74, 4992, 71, 4992, 88, 5040, 76, 5088, 79, 5088, 90, 5136, 76, 5136, 91, 5184, 71, 5232, 76, 5280, 79, 5280, 88, 5328, 76, 5328, 85, 5376, 69, 5424, 73, 5472, 79, 5472, 86, 5520, 73, 5520, 88, 5568, 69, 5616, 73, 5616, 81, 5664, 79, 5664, 86, 5712, 73, 5712, 88, 5760, 70, 5760, 89, 5808, 74, 5808, 88, 5856, 77, 5856, 86, 5904, 81, 5904, 84, 6048, 81, 6096, 82, 6141, 89, 6144, 105, 6144, 72, 6144, 77, 6144, 84, 6192, 81, 6192, 96, 6240, 108, 6240, 72, 6240, 77, 6240, 89, 6288, 96, 6336, 103, 6336, 72, 6336, 76, 6336, 88, 6336, 88, 6384, 79, 6384, 86, 6384, 96, 6432, 108, 6432, 72, 6432, 76, 6432, 86, 6480, 84, 6480, 96, 6528, 106, 6528, 70, 6528, 74, 6528, 86, 6528, 86, 6576, 105, 6576, 82, 6576, 84, 6576, 96, 6624, 103, 6624, 70, 6624, 74, 6624, 84, 6672, 76, 6672, 96, 6720, 105, 6720, 72, 6720, 77, 6720, 84, 6720, 89, 6768, 81, 6768, 96, 6816, 101, 6816, 72, 6816, 77, 6816, 81, 6864, 82, 6864, 96, 6909, 89, 6912, 105, 6912, 72, 6912, 77, 6912, 84, 6960, 81, 6960, 96, 7008, 108, 7008, 72, 7008, 77, 7008, 89, 7056, 96, 7104, 103, 7104, 72, 7104, 76, 7104, 88, 7104, 91, 7152, 79, 7152, 89, 7152, 96, 7200, 108, 7200, 72, 7200, 76, 7200, 88, 7248, 86, 7248, 96, 7296, 106, 7296, 70, 7296, 74, 7296, 86, 7296, 86, 7344, 105, 7344, 82, 7344, 88, 7344, 96, 7392, 103, 7392, 70, 7392, 74, 7392, 89, 7440, 76, 7440, 96, 7488, 105, 7488, 72, 7488, 77, 7488, 89, 7488, 89, 7536, 81, 7536, 96, 7584, 101, 7584, 72, 7584, 77, 7584, 91, 7632, 93, 7632, 96, 7680, 103, 7680, 70, 7680, 74, 7680, 91, 7680, 94, 7728, 106, 7728, 79, 7728, 94, 7776, 105, 7776, 70, 7776, 74, 7776, 93, 7824, 103, 7824, 79, 7872, 100, 7872, 72, 7872, 76, 7872, 91, 7872, 91, 7920, 101, 7920, 79, 7968, 103, 7968, 72, 7968, 76, 7968, 89, 8016, 108, 8016, 79, 8016, 91, 8064, 105, 8064, 72, 8064, 77, 8064, 89, 8064, 93, 8112, 108, 8112, 81, 8112, 93, 8160, 103, 8160, 72, 8160, 77, 8160, 88, 8160, 91, 8208, 108, 8208, 81, 8256, 101, 8256, 74, 8256, 77, 8256, 86, 8256, 89, 8304, 81, 8304, 93, 8352, 105, 8352, 74, 8352, 77, 8352, 86, 8400, 81, 8400, 84, 8445, 89, 8448, 106, 8448, 70, 8448, 74, 8448, 86, 8496, 79, 8496, 89, 8496, 98, 8544, 101, 8544, 70, 8544, 74, 8544, 89, 8592, 79, 8592, 88, 8592, 98, 8640, 105, 8640, 69, 8640, 73, 8640, 88, 8688, 79, 8688, 88, 8688, 97, 8736, 103, 8736, 69, 8736, 73, 8736, 74, 8736, 90, 8784, 102, 8784, 79, 8784, 90, 8832, 62, 8832, 69, 8880, 74, 8928, 78, 8976, 62, 8976, 74, 9024, 59, 9024, 71, 9072, 74, 9120, 78, 9168, 59, 9168, 74, 9216, 62, 9216, 69, 9264, 74, 9312, 78, 9360, 62, 9360, 74, 9408, 59, 9408, 71, 9456, 74, 9504, 78, 9552, 59, 9552, 74, 9600, 62, 9600, 69, 9648, 74, 9696, 78, 9744, 62, 9744, 74, 9792, 59, 9792, 71, 9840, 74, 9888, 78, 9936, 59, 9936, 74, 9984, 62, 9984, 69, 10032, 74, 10080, 78, 10128, 62, 10128, 74, 10128, 81, 10176, 71, 10176, 71, 10176, 91, 10224, 74, 10224, 90, 10272, 71, 10272, 78, 10272, 88, 10320, 69, 10320, 74, 10320, 88, 10368, 62, 10368, 69, 10368, 90, 10416, 74, 10464, 78, 10512, 62, 10512, 74, 10560, 59, 10560, 71, 10608, 74, 10656, 78, 10704, 59, 10704, 74, 10752, 62, 10752, 69, 10800, 74, 10848, 78, 10896, 62, 10896, 74, 10944, 59, 10944, 71, 10944, 91, 10992, 74, 10992, 90, 11040, 78, 11040, 88, 11088, 59, 11088, 74, 11088, 88, 11136, 62, 11136, 69, 11184, 74, 11232, 78, 11232, 90, 11280, 62, 11280, 74, 11280, 86, 11328, 59, 11328, 71, 11376, 74, 11376, 88, 11424, 78, 11472, 59, 11472, 74, 11472, 81, 11520, 62, 11520, 69, 11568, 74, 11616, 78, 11664, 62, 11664, 74, 11712, 59, 11712, 71, 11760, 74, 11808, 78, 11856, 59, 11856, 74, 11904, 64, 11904, 71, 11904, 88, 11952, 76, 12000, 79, 12000, 90, 12048, 64, 12048, 76, 12048, 91, 12096, 64, 12096, 71, 12144, 66, 12144, 76, 12192, 67, 12192, 79, 12192, 88, 12240, 76, 12288, 57, 12288, 69, 12288, 85, 12336, 73, 12384, 79, 12384, 86, 12432, 57, 12432, 73, 12432, 88, 12480, 57, 12480, 69, 12528, 59, 12528, 73, 12576, 61, 12576, 79, 12576, 81, 12624, 73, 12624, 81, 12672, 62, 12672, 69, 12720, 74, 12720, 90, 12768, 78, 12816, 62, 12816, 74, 12864, 59, 12864, 71, 12912, 74, 12960, 78, 13008, 59, 13008, 74, 13056, 62, 13056, 69, 13104, 74, 13152, 78, 13200, 62, 13200, 74, 13200, 81, 13200, 86, 13248, 71, 13248, 71, 13248, 91, 13248, 95, 13296, 74, 13296, 90, 13296, 93, 13344, 71, 13344, 78, 13344, 88, 13344, 91, 13392, 69, 13392, 74, 13392, 88, 13392, 91, 13440, 62, 13440, 69, 13440, 90, 13440, 93, 13488, 74, 13536, 78, 13584, 62, 13584, 74, 13632, 59, 13632, 71, 13680, 74, 13728, 78, 13776, 59, 13776, 74, 13824, 62, 13824, 69, 13872, 74, 13920, 78, 13968, 62, 13968, 74, 13968, 81, 13968, 86, 14016, 59, 14016, 71, 14016, 91, 14016, 95, 14064, 74, 14064, 90, 14064, 93, 14112, 78, 14112, 88, 14112, 91, 14160, 59, 14160, 74, 14160, 88, 14160, 91, 14208, 62, 14208, 69, 14256, 74, 14304, 78, 14304, 90, 14304, 93, 14352, 62, 14352, 74, 14352, 86, 14352, 90, 14400, 59, 14400, 71, 14448, 74, 14496, 78, 14496, 88, 14496, 91, 14544, 59, 14544, 74, 14544, 81, 14544, 86, 14592, 62, 14592, 69, 14640, 74, 14688, 78, 14736, 62, 14736, 74, 14784, 59, 14784, 71, 14832, 74, 14880, 78, 14928, 59, 14928, 74, 14976, 64, 14976, 71, 14976, 88, 15024, 76, 15072, 79, 15072, 90, 15120, 64, 15120, 76, 15120, 91, 15168, 64, 15168, 71, 15216, 66, 15216, 76, 15264, 67, 15264, 79, 15264, 88, 15312, 76, 15360, 57, 15360, 69, 15360, 85, 15408, 73, 15456, 79, 15456, 86, 15504, 57, 15504, 73, 15504, 88, 15552, 57, 15552, 69, 15600, 59, 15600, 73, 15600, 81, 15648, 61, 15648, 79, 15648, 86, 15696, 57, 15696, 73, 15696, 88, 15744, 58, 15744, 70, 15744, 89, 15792, 74, 15792, 88, 15840, 81, 15840, 86, 15888, 70, 15888, 77, 15888, 84, 15936, 81, 15984, 77, 16032, 70, 16032, 77, 16032, 81, 16032, 81, 16080, 57, 16080, 77, 16080, 79, 16080, 82, 16128, 65, 16128, 72, 16128, 77, 16128, 81, 16128, 84, 16176, 81, 16224, 72, 16224, 77, 16224, 84, 16224, 89, 16272, 65, 16320, 60, 16320, 72, 16320, 76, 16320, 84, 16320, 88, 16368, 79, 16368, 86, 16416, 72, 16416, 76, 16416, 82, 16416, 86, 16464, 60, 16464, 81, 16464, 84, 16512, 58, 16512, 70, 16512, 74, 16512, 82, 16512, 86, 16560, 81, 16560, 82, 16560, 84, 16608, 70, 16608, 74, 16608, 81, 16608, 84, 16656, 58, 16656, 76, 16704, 65, 16704, 72, 16704, 77, 16704, 81, 16704, 84, 16752, 81, 16800, 72, 16800, 77, 16800, 79, 16800, 81, 16848, 77, 16848, 82, 16896, 65, 16896, 72, 16896, 77, 16896, 77, 16896, 84, 16944, 81, 16992, 72, 16992, 77, 16992, 81, 16992, 89, 17040, 65, 17088, 60, 17088, 72, 17088, 76, 17088, 82, 17088, 91, 17136, 79, 17136, 81, 17136, 89, 17184, 72, 17184, 76, 17184, 79, 17184, 88, 17232, 60, 17232, 77, 17232, 86, 17280, 58, 17280, 70, 17280, 74, 17280, 77, 17280, 86, 17328, 79, 17328, 82, 17328, 88, 17376, 70, 17376, 74, 17376, 81, 17376, 89, 17424, 58, 17424, 76, 17472, 65, 17472, 72, 17472, 77, 17472, 81, 17472, 89, 17520, 81, 17568, 65, 17568, 72, 17568, 77, 17568, 82, 17568, 91, 17616, 60, 17616, 84, 17616, 93, 17664, 58, 17664, 70, 17664, 74, 17664, 86, 17664, 94, 17712, 79, 17712, 86, 17712, 94, 17760, 58, 17760, 70, 17760, 74, 17760, 84, 17760, 93, 17808, 79, 17856, 60, 17856, 72, 17856, 76, 17856, 84, 17856, 91, 17904, 79, 17952, 60, 17952, 72, 17952, 76, 17952, 81, 17952, 89, 18000, 79, 18000, 82, 18000, 91, 18048, 65, 18048, 72, 18048, 77, 18048, 84, 18048, 93, 18096, 81, 18096, 84, 18096, 93, 18144, 64, 18144, 72, 18144, 77, 18144, 82, 18144, 91, 18192, 81, 18192, 89, 18240, 62, 18240, 74, 18240, 77, 18240, 81, 18240, 89, 18288, 81, 18336, 60, 18336, 74, 18336, 77, 18336, 79, 18336, 86, 18384, 77, 18384, 81, 18384, 84, 18432, 58, 18432, 70, 18432, 74, 18432, 77, 18432, 86, 18480, 79, 18480, 79, 18480, 89, 18528, 65, 18528, 70, 18528, 74, 18528, 81, 18528, 89, 18576, 79, 18576, 79, 18576, 88, 18624, 57, 18624, 69, 18624, 73, 18672, 79, 18672, 79, 18672, 88, 18720, 64, 18720, 69, 18720, 73, 18720, 81, 18720, 90, 18768, 79, 18768, 81, 18768, 90, 18816, 62, 18816, 69, 18864, 74, 18912, 78, 18960, 62, 18960, 74, 19008, 59, 19008, 71, 19056, 74, 19104, 78, 19152, 59, 19152, 74, 19200, 62, 19200, 69, 19248, 74, 19296, 78, 19344, 62, 19344, 74, 19392, 59, 19392, 71, 19440, 74, 19488, 78, 19536, 59, 19536, 74, 19584, 62, 19584, 69, 19632, 74, 19680, 78, 19728, 62, 19728, 74, 19776, 59, 19776, 71, 19824, 74, 19872, 78, 19920, 59, 19920, 74, 19968, 62, 19968, 69, 20016, 74, 20064, 78, 20112, 62, 20112, 74, 20160, 71, 20160, 71, 20160, 91, 20184, 91, 20208, 74, 20208, 90, 20256, 71, 20256, 78, 20256, 88, 20304, 69, 20304, 74, 20304, 88, 20352, 69, 20376, 90, 20400, 74, 20448, 78, 20496, 74, 20544, 71, 20592, 74, 20640, 78, 20688, 74, 20736, 69, 20784, 74, 20832, 78, 20880, 74, 20880, 81, 20928, 71, 20928, 91, 20976, 74, 20976, 90, 21024, 78, 21024, 88, 21072, 74, 21072, 88, 21120, 69, 21168, 74, 21216, 78, 21216, 90, 21264, 74, 21264, 86, 21312, 71, 21360, 74, 21408, 78, 21408, 88, 21456, 74, 21456, 81, 21504, 69, 21552, 74, 21600, 78, 21648, 74, 21696, 71, 21744, 74, 21792, 78, 21840, 74, 21888, 71, 21888, 88, 21936, 76, 21984, 79, 21984, 90, 22032, 76, 22032, 91, 22080, 71, 22128, 76, 22176, 79, 22176, 88, 22224, 76, 22272, 69, 22272, 85, 22320, 73, 22368, 79, 22368, 86, 22416, 73, 22416, 88, 22464, 69, 22512, 73, 22560, 79, 22560, 81, 22608, 73, 22608, 81, 22656, 69, 22704, 74, 22704, 90, 22752, 78, 22800, 74, 22848, 71, 22896, 74, 22944, 78, 22992, 74, 23040, 69, 23088, 74, 23136, 78, 23184, 74, 23232, 71, 23232, 91, 23232, 95, 23280, 74, 23280, 90, 23280, 93, 23328, 78, 23328, 88, 23328, 91, 23376, 74, 23376, 88, 23376, 91, 23424, 62, 23424, 69, 23436, 90, 23436, 93, 23472, 74, 23520, 78, 23568, 62, 23568, 74, 23616, 59, 23616, 71, 23664, 74, 23712, 78, 23760, 59, 23760, 74, 23808, 62, 23808, 69, 23856, 74, 23904, 78, 23952, 62, 23952, 74, 24000, 59, 24000, 71, 24000, 91, 24000, 95, 24048, 74, 24048, 90, 24048, 93, 24096, 78, 24096, 88, 24096, 91, 24144, 59, 24144, 74, 24144, 88, 24144, 91, 24192, 62, 24192, 69, 24240, 74, 24288, 78, 24288, 90, 24288, 93, 24336, 62, 24336, 74, 24336, 86, 24336, 90, 24384, 59, 24384, 71, 24432, 74, 24480, 78, 24480, 88, 24480, 91, 24528, 59, 24528, 74, 24528, 81, 24528, 86, 24576, 62, 24576, 69, 24624, 74, 24672, 78, 24720, 62, 24720, 74, 24768, 59, 24768, 71, 24816, 74, 24864, 78, 24912, 59, 24912, 74, 24960, 64, 24960, 71, 24960, 88, 25008, 76, 25056, 79, 25056, 90, 25104, 64, 25104, 76, 25104, 91, 25152, 64, 25152, 71, 25200, 66, 25200, 76, 25248, 67, 25248, 79, 25248, 88, 25296, 76, 25344, 57, 25344, 69, 25344, 85, 25392, 73, 25440, 79, 25440, 86, 25488, 57, 25488, 73, 25488, 88, 25536, 57, 25536, 69, 25584, 59, 25584, 73, 25584, 81, 25632, 61, 25632, 79, 25632, 86, 25680, 57, 25680, 73, 25680, 88, 25728, 58, 25728, 70, 25728, 89, 25776, 74, 25776, 88, 25824, 81, 25824, 86, 25872, 70, 25872, 77, 25872, 84, 25920, 81, 25968, 77, 26016, 70, 26016, 77, 26016, 81, 26016, 81, 26064, 57, 26064, 77, 26064, 79, 26064, 82, 26112, 65, 26112, 72, 26112, 77, 26112, 81, 26112, 84, 26160, 81, 26208, 72, 26208, 77, 26208, 84, 26208, 89, 26256, 65, 26304, 60, 26304, 72, 26304, 76, 26304, 84, 26304, 88, 26352, 79, 26352, 86, 26400, 72, 26400, 76, 26400, 82, 26400, 86, 26448, 60, 26448, 81, 26448, 84, 26496, 58, 26496, 70, 26496, 74, 26496, 82, 26496, 86, 26544, 81, 26544, 82, 26544, 84, 26592, 70, 26592, 74, 26592, 81, 26592, 84, 26640, 58, 26640, 76, 26688, 65, 26688, 72, 26688, 77, 26688, 81, 26688, 84, 26736, 81, 26784, 72, 26784, 77, 26784, 79, 26784, 81, 26832, 65, 26832, 77, 26832, 82, 26880, 65, 26880, 72, 26880, 77, 26880, 77, 26880, 84, 26928, 81, 26976, 72, 26976, 77, 26976, 81, 26976, 89, 27024, 65, 27072, 60, 27072, 72, 27072, 76, 27072, 82, 27072, 91, 27120, 79, 27120, 81, 27120, 89, 27168, 72, 27168, 76, 27168, 79, 27168, 88, 27216, 60, 27216, 77, 27216, 86, 27264, 58, 27264, 70, 27264, 74, 27264, 77, 27264, 86, 27312, 79, 27312, 82, 27312, 88, 27360, 70, 27360, 74, 27360, 81, 27360, 89, 27408, 58, 27408, 76, 27456, 65, 27456, 72, 27456, 77, 27456, 81, 27456, 89, 27504, 81, 27552, 65, 27552, 72, 27552, 77, 27552, 82, 27552, 91, 27600, 60, 27600, 84, 27600, 93, 27648, 58, 27648, 70, 27648, 74, 27648, 86, 27648, 94, 27696, 79, 27696, 86, 27696, 94, 27744, 58, 27744, 70, 27744, 74, 27744, 84, 27744, 93, 27792, 79, 27792, 91, 27840, 60, 27840, 72, 27840, 76, 27840, 84, 27840, 91, 27888, 79, 27936, 60, 27936, 72, 27936, 76, 27936, 81, 27936, 89, 27984, 79, 27984, 82, 27984, 91, 28032, 65, 28032, 72, 28032, 77, 28032, 84, 28032, 93, 28080, 81, 28080, 84, 28080, 93, 28128, 64, 28128, 72, 28128, 77, 28128, 82, 28128, 91, 28176, 81, 28176, 89, 28224, 62, 28224, 74, 28224, 77, 28224, 81, 28224, 89, 28272, 81, 28320, 60, 28320, 74, 28320, 77, 28320, 79, 28320, 86, 28368, 77, 28368, 81, 28368, 84, 28416, 58, 28416, 70, 28416, 74, 28416, 77, 28416, 86, 28464, 79, 28464, 79, 28464, 89, 28512, 65, 28512, 70, 28512, 74, 28512, 81, 28512, 89, 28560, 79, 28560, 79, 28560, 88, 28608, 57, 28608, 69, 28608, 73, 28656, 79, 28656, 79, 28656, 88, 28704, 64, 28704, 69, 28704, 73, 28704, 81, 28704, 90, 28752, 79, 28752, 81, 28752, 90, 28800, 62, 28800, 69, 28848, 74, 28896, 78, 28944, 62, 28944, 74, 28992, 59, 28992, 71, 29040, 74, 29088, 78, 29088, 93, 29136, 59, 29136, 74, 29136, 93, 29184, 62, 29184, 69, 29184, 95, 29232, 74, 29232, 93, 29280, 78, 29280, 90, 29328, 62, 29328, 74, 29328, 86, 29376, 59, 29376, 71, 29424, 74, 29424, 88, 29424, 91, 29472, 78, 29472, 90, 29472, 93, 29520, 59, 29520, 74, 29520, 90, 29520, 93, 29568, 62, 29568, 69, 29616, 74, 29664, 78, 29712, 62, 29712, 74, 29760, 59, 29760, 71, 29808, 74, 29808, 93, 29856, 78, 29856, 93, 29904, 59, 29904, 74, 29904, 93, 29952, 62, 29952, 69, 29952, 95, 30000, 74, 30000, 93, 30048, 78, 30048, 90, 30096, 62, 30096, 74, 30096, 86, 30144, 59, 30144, 71, 30192, 74, 30192, 88, 30192, 91, 30240, 78, 30240, 90, 30240, 93, 30288, 59, 30288, 74, 30288, 90, 30288, 93, 30336, 62, 30336, 69, 30384, 74, 30432, 78, 30480, 62, 30480, 74, 30528, 59, 30528, 71, 30576, 74, 30576, 93, 30624, 78, 30624, 93, 30672, 59, 30672, 74, 30672, 93, 30720, 62, 30720, 69, 30720, 95, 30768, 74, 30768, 93, 30816, 78, 30816, 90, 30864, 62, 30864, 74, 30864, 86, 30912, 59, 30912, 71, 30960, 74, 30960, 88, 30960, 91, 31008, 78, 31008, 90, 31008, 93, 31056, 59, 31056, 74, 31056, 90, 31056, 93, 31104, 62, 31104, 69, 31152, 74, 31200, 78, 31248, 62, 31248, 74, 31296, 59, 31296, 71, 31344, 74, 31392, 78, 31392, 93, 31440, 59, 31440, 74, 31440, 93, 31488, 62, 31488, 69, 31488, 95, 31536, 74, 31536, 93, 31584, 78, 31584, 90, 31632, 62, 31632, 74, 31632, 86, 31680, 59, 31680, 71, 31728, 74, 31728, 88, 31728, 91, 31776, 78, 31776, 90, 31776, 93, 31824, 59, 31824, 74, 31824, 90, 31824, 93, 31872, 62, 31872, 69, 31920, 74, 31968, 78, 32016, 62, 32016, 74, 32064, 59, 32064, 71, 32112, 74, 32112, 93, 32160, 78, 32160, 93, 32208, 59, 32208, 74, 32208, 93, 32256, 62, 32256, 69, 32256, 95, 32304, 74, 32304, 93, 32352, 78, 32352, 90, 32400, 62, 32400, 74, 32400, 86, 32448, 59, 32448, 71, 32496, 74, 32496, 88, 32496, 91, 32544, 78, 32544, 90, 32544, 93, 32592, 59, 32592, 74, 32592, 90, 32592, 93, 32640, 62, 32640, 69, 32688, 74, 32736, 78, 32784, 62, 32784, 74, 32832, 59, 32832, 71, 32880, 74, 32880, 91, 32928, 78, 32928, 93, 32976, 59, 32976, 74, 32976, 93, 33024, 62, 33024, 69, 33072, 74, 33120, 78, 33168, 62, 33168, 74, 33216, 71, 33216, 71, 33264, 74, 33264, 91, 33312, 71, 33312, 78, 33312, 90, 33360, 69, 33360, 74, 33360, 90, 33791, 0 // 176 seconds * 48 * 4 - 1 };

    synth.asm

    .cdecls C, LIST, "msp430fr5739.h" .text .def set_tick .def get_tick .def synth_init .def set_note .ref wave .bss tick, 2 .bss pwm_out, 2 .bss phase_inc, 6 * 8 ; Level ; Phase increment LSW/MSW (from note table) .bss phase_acc, 4 * 8 ; Phase accumulator LSW/MSW .def phase_inc set_tick: mov R12, &tick reta get_tick: mov &tick, R12 reta synth_init: mov #0x0210, &TA0CTL ; Timer A config: SMCLK, count up mov #749, &TA0CCR0 ; Setup Timer A period for 32000 sps mov #375, &TA0CCR1 ; Setup Timer A compare mov #0x00E0, &TA0CCTL1 ; Setup Timer A reset/set output mode ; mov #phase_inc, R12 ; Clear all phase inc and accum mov #5 * 16, R14 ; Word count clr 0(R12) ; Clear word incd R12 ; Next word dec R14 ; Dec word count jne $ - 8 ; Loop until all words done... ; eint ; Enable interupts bis #0x0010, &TA0CCTL0 ; Enable PWM interupt ; reta ; ; synth_isr: ; mov &pwm_out, &TA0CCR1 ; Output sample ; push R4 ; Wavetable pointer push R5 ; Phase increment / level pointer push R6 ; Phase accumulator pointer push R8 ; Wave sample pointer / next sample push R9 ; Wave sample push R10 ; Voice mix accumulator MSW push R11 ; Voice mix accumulator LSW ; mov #wave, R4 ; Setup wavetable pointer mov #phase_inc, R5 ; Setup phase increment and level pointer mov #phase_acc, R6 ; Setup phase accumulator pointer clr R10 ; Clear voice mix clr R11 ; voice_loop: ; .loop 8 mov @R6+, &MPYS32L ; Get phase acc LSW (fraction) to multiplier mov @R6+, R8 ; Get phase acc MSW (wave table index) ; bit #0xFF00, R8 ; Test phase acc MSB mov.b R8, R8 ; Clear MSB ;jeq no_decay ; jeq $ + 22 mov @R5, &MPY ; Get level mov #0xFF00, &OP2 ; Decay factor mov R8, -2(R6) ; Update phase acc mov &RES1, 0(R5) ; Update level ;no_decay: clr &MPYS32H ; add R8, R8 ; Make word index add R4, R8 ; Add wavetable pointer mov @R8+, R9 ; Get wave sample mov @R8, R8 ; Get next wave sample sub R9, R8 ; Calc delta mov R8, &OP2L ; Multiply by delta bit #0x8000, R8 ; Sign extend delta subc R8, R8 ; inv R8 ; mov R8, &OP2H ; add &RES1, R9 ; Add interpolation to sample mov R9, &MPYS ; Multiply by voice level mov @R5+, &OP2 ; add @R5+, -4(R6) ; Update phase acc addc @R5+, -2(R6) ; add &RES0, R11 ; Update mix addc &RES1, R10 ; ; .endloop ; swpb R10 ; Use MSB of R10 mov.b R10, R10 ; sxt R10 ; Sign extend add R10, R10 ; * 2 add #375, R10 ; Bias to center of PWM range mov R10, &pwm_out ; ; dec &tick ; jc $ + 6 ; clr &tick ; ; pop R11 ; pop R10 ; pop R9 ; pop R8 ; pop R6 ; pop R5 ; pop R4 ; reti ; ; set_note: ; mov R12, R15 ; Voice * 6 add R15, R12 ; (+1 = *2) add R15, R12 ; (+1 = *3) rla R12 ; (*2 = *6) add #phase_inc, R12 ; Add phase inc pointer mov R14, 0(R12) ; Set level cmp #128, R13 ; Out of range note values are note off jhs note_off ; clr R14 ; Clear octave count tst_note: ; cmp #116, R13 ; Within note table? jge get_pi ; Yes... inc R14 ; Inc octave count add #12, R13 ; Add octave to note jmp tst_note ; Check again... get_pi: ; Get phase increment sub #116, R13 ; Adjust for first note in table rla R13 ; MIDI note * 4 rla R13 ; add #notes, R13 ; Add note table pointer mov @R13+, R15 ; Get LSW mov @R13, R13 ; Get MSW tst R14 ; Shifting required? jeq set_phase ; No... shift_phase: ; rra R13 ; Shift phase inc rrc R15 ; dec R14 ; Dec octave count jne shift_phase ; Repeat until zero... set_phase: ; mov R15, 2(R12) ; Set phase inc mov R13, 4(R12) ; reta ; Return ; note_off: ; incd SP ; Discard level clr 0(R12) ; Clear phase inc clr 2(R12) ; .if 0 ; Note: Abrupt return to zero causes poping clr 4(R12) ; Clear level add #phase_acc - phase_inc, R12 ; Phase accum pointer clr 0(R12) ; Clear phase accum clr 2(R12) ; .endif ; reta ; Return ; ; notes ; MIDI Note Frequency .long 3483828 ; 116 G#8 6644.87457275391 .long 3690988 ; 117 A8 7040.00091552734 .long 3910465 ; 118 A#8 7458.62007141113 .long 4142993 ; 119 B8 7902.13203430176 .long 4389349 ; 120 C9 8372.01881408691 .long 4650353 ; 121 C#9 8869.84443664551 .long 4926877 ; 122 D9 9397.27210998535 .long 5219845 ; 123 D#9 9956.06422424316 .long 5530233 ; 124 E9 10548.0823516846 .long 5859077 ; 125 F9 11175.3025054932 .long 6207476 ; 126 F#9 11839.8208618164 .long 6576592 ; 127 G9 12543.8537597656 ; Interrupt Vectors .sect ".int53" ; TA0CCR0 CCIFG0 .short synth_isr ; ; .end ;
  25. Like
    oPossum got a reaction from tripwire in music with assembly language   
    A variation of the RTTL player that adds a simple envelope to each note.
     

     
     
     
    Some in assembly & C:
     
    MIDI driven:
     

     
    http://forum.43oh.com/topic/810-fraunchpad-poly-synth/
     
    This has a bug that causes only half of the sine wave to be used. Look carefully at the 'scope and you will see it.
     
     
    Table driven:
     
    This has a bug that causes the frequency to be a bit off. Don't remember if I fixed the posted code.
     

     

     
    http://forum.43oh.com/topic/1729-fraunchpad-synth-still-alive/
×
×
  • Create New...