Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Problem with Serial.print after adding FOR loop on: August 01, 2014, 12:47:33 am
41 * 256 = ?

does it help if i were to store in the ROM memory of the microcontroller?
2  Using Arduino / Programming Questions / Problem with Serial.print after adding FOR loop on: July 31, 2014, 11:51:26 pm
The correct way the code shall work is that it will serial.print the contents of 2D array variable Y and after that compare the contents of 2D array Y with another predefined 2D array buffer.

As u can see in my code, there are 2 sets of nested FOR loop. With 2 sets of nested FOR loop, the code doesn't work. However, when I comment away the 2nd nested FOR loop, it works perfectly!

The 2 sets of nested FOR loops have no relation to each other as u can see. What is the problem? Can someone help me out?

Code:
void loop() {
  byte y[41][256]={
    0    };
  int a;
  int b;
  for (a=0;a<41;a++){
    for (b=0;b<256;b++){
      Serial.print("Address:|");
      Serial.print(address);
      Serial.print("| - Value:|");
      y[a][b]=disk1.read_byte(address);
      Serial.print(y[a][b], DEC);
      Serial.print("|\n");
      address++;
      delay(100);
    }

    if (address == 10495)
    {
      address = 0;
    }
  }

  for (a=0;a<41;a++){
    for(b=0;b<256;b++){
      if (y[a][b]==buffer[a][b]){
        Serial.print("OK");
      }
      else{
        Serial.print("NO");
      }
    }
  }
}

3  Using Arduino / Programming Questions / Re: SPI master & slave simultaneously on: May 06, 2014, 04:05:10 am
Thanks jack. This is great help. Back to working on the codes  smiley-confuse
4  Using Arduino / Programming Questions / SPI master & slave simultaneously on: May 06, 2014, 03:42:33 am
I would like to find out if it is possible to program my 328p as both a SPI master and SPI slave within the same program.

It does not necessarily work as master and slave "at the same time".

Here's the scenario:

The microcontroller (slave) shall receive command from another master. Upon receiving the command, it shall access a EEPROM using SPI. I understand that in order to retrieve data from EEPROM, the microcontroller needs to become master.

Is the above scenario possible?

Thanks for reading
5  Using Arduino / Programming Questions / How to compile source code downloaded from GitHub on: January 16, 2014, 09:22:39 pm
Dear all,

I am working on a project to integrate M25P16 EEPROM with arduino328p.

Thankfully, I was able to find a sample code online at:
https://github.com/contiki/contiki-arduino/blob/master/platform/sensinode/dev/m25p16.c

However, I have been having a hard time compiling the code on the arduino IDE. I had tried adding the libraries and various forms of editing to make it work but to no avail.

Can any kind soul out there tell me the instructions to successfully upload the program to my dev board?

This is the link to download the source code:
https://github.com/contiki-os/contiki/archive/master.zip

Code:
#include "dev/n740.h"
#include "dev/m25p16.h"
#include "sys/clock.h"
#include "sys/energest.h"
#include "cc2430_sfr.h"

#define CLOCK_RISING()  {M25P16_PIN_CLOCK = 1; M25P16_PIN_CLOCK = 0;}
#define CLOCK_FALLING() {M25P16_PIN_CLOCK = 0; M25P16_PIN_CLOCK = 1;}
/*---------------------------------------------------------------------------*/
/* Bit-Bang write a byte to the chip */
static void
bit_bang_write(uint8_t byte) CC_NON_BANKED
{
  uint8_t i;
  uint8_t bit;

  /* bit-by-bit */
  for(i = 0x80; i > 0; i >>= 1) {
    /* Is the bit set? */
    bit = 0;
    if(i & byte) {
      /* If it was set, we want to send 1 */
      bit = 1;
    }
    /* Send the bit */
    M25P16_PIN_SER_I = bit;
    /* Clock - Rising */
    CLOCK_RISING();
  }
}
/*---------------------------------------------------------------------------*/
/* Bit-Bang read a byte from the chip */
static uint8_t
bit_bang_read() CC_NON_BANKED
{
  int8_t i;
  uint8_t bits = 0;

  /* bit-by-bit */
  for(i = 7; i >= 0; i--) {
    /* Clock - Falling */
    CLOCK_FALLING();

    /* Read the bit */
    bits |= (M25P16_PIN_SER_O << i);
  }
  return bits;
}
/*---------------------------------------------------------------------------*/
static void
select() CC_NON_BANKED
{
  /* Read current ser/par value */
  uint8_t ser_par = n740_ser_par_get();

  M25P16_PIN_CLOCK = 0;

  ser_par &= ~N740_SER_PAR_CHIP_SEL;  /* Select Flash */

  /* Write the new status back to the ser/par */
  n740_ser_par_set(ser_par);
}
/*---------------------------------------------------------------------------*/
static void
deselect() CC_NON_BANKED
{
  /* Read current ser/par value */
  uint8_t ser_par = n740_ser_par_get();

  ser_par |= N740_SER_PAR_CHIP_SEL;    /* De-Select Flash  */

  /* Write the new status back to the ser/par */
  n740_ser_par_set(ser_par);
}
/*---------------------------------------------------------------------------*/
void
m25p16_wren()
{
  select();
  bit_bang_write(M25P16_I_WREN);
  deselect();

  while(!M25P16_WEL());
}
/*---------------------------------------------------------------------------*/
void
m25p16_wrdi()
{
  select();
  bit_bang_write(M25P16_I_WRDI);
  deselect();
}
/*---------------------------------------------------------------------------*/
void
m25p16_rdid(struct m25p16_rdid * rdid)
{
  uint8_t i;

  select();
  bit_bang_write(M25P16_I_RDID);

  rdid->man_id = bit_bang_read();
  rdid->mem_type = bit_bang_read(); /* Device ID MSB */
  rdid->mem_size = bit_bang_read(); /* Device ID LSB */
  rdid->uid_len = bit_bang_read();
  for(i = 0; i < rdid->uid_len; i++) {
    rdid->uid[i] = bit_bang_read();
  }
  deselect();
}
/*---------------------------------------------------------------------------*/
uint8_t
m25p16_rdsr()
{
  uint8_t rv;

  select();
  bit_bang_write(M25P16_I_RDSR);
  rv = bit_bang_read();
  deselect();

  return rv;
}
/*---------------------------------------------------------------------------*/
void
m25p16_wrsr(uint8_t val)
{
  m25p16_wren(); /* Write Enable */

  select();
  ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
  bit_bang_write(M25P16_I_WRSR);
  bit_bang_write(val);
  ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
  deselect();
}
/*---------------------------------------------------------------------------*/
void
m25p16_read(uint8_t * addr, uint8_t * buff, uint8_t buff_len)
{
  uint8_t i;

  select();
  ENERGEST_ON(ENERGEST_TYPE_FLASH_READ);

#if M25P16_READ_FAST
  bit_bang_write(M25P16_I_FAST_READ);
#else
  bit_bang_write(M25P16_I_READ);
#endif

  /* Write the address, MSB in addr[0], bits [7:5] of the MSB: 'don't care' */
  for(i = 0; i < 3; i++) {
    bit_bang_write(addr[i]);
  }

  /* For FAST_READ, send the dummy byte */
#if M25P16_READ_FAST
  bit_bang_write(M25P16_DUMMY_BYTE);
#endif

  for(i = 0; i < buff_len; i++) {
    buff[i] = ~bit_bang_read();
  }
  ENERGEST_OFF(ENERGEST_TYPE_FLASH_READ);
  deselect();
}
/*---------------------------------------------------------------------------*/
void
m25p16_pp(uint8_t * addr, uint8_t * buff, uint8_t buff_len)
{
  uint8_t i;

  m25p16_wren(); /* Write Enable */

  select();
  ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
  bit_bang_write(M25P16_I_PP);

  /* Write the address, MSB in addr[0] */
  for(i = 0; i < 3; i++) {
    bit_bang_write(addr[i]);
  }

  /* Write the bytes */
  for(i=0; i<buff_len; i++) {
    bit_bang_write(~buff[i]);
  }
  ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
  deselect();
}
/*---------------------------------------------------------------------------*/
void
m25p16_se(uint8_t s)
{
  m25p16_wren(); /* Write Enable */

  select();
  ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE);
  bit_bang_write(M25P16_I_SE);
  bit_bang_write(s);
  bit_bang_write(0x00);
  bit_bang_write(0x00);
  deselect();
  ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE);
}
/*---------------------------------------------------------------------------*/
void
m25p16_be()
{
  m25p16_wren(); /* Write Enable */

  select();
  bit_bang_write(M25P16_I_BE);
  deselect();
}
/*---------------------------------------------------------------------------*/
void
m25p16_dp()
{
  select();
  bit_bang_write(M25P16_I_DP);
  deselect();
}
/*---------------------------------------------------------------------------*/
/*
 * Release Deep Power Down. We do NOT read the Electronic Signature
 */
void
m25p16_res() {
  select();
  bit_bang_write(M25P16_I_RES);
  deselect();
  /* a few usec between RES and standby */
  while(M25P16_WIP());
}
/*---------------------------------------------------------------------------*/
/**
 * Release Deep Power Down. Read and return the Electronic Signature
 * must return 0x14
 *
 * \return The old style Electronic Signature. This must be 0x14
 */
uint8_t
m25p16_res_res() {
  uint8_t rv;

  select();
  bit_bang_write(M25P16_I_RES);
  bit_bang_write(M25P16_DUMMY_BYTE);
  bit_bang_write(M25P16_DUMMY_BYTE);
  bit_bang_write(M25P16_DUMMY_BYTE);

  rv = bit_bang_read();

  deselect();

  /* a few usec between RES and standby */
  while(M25P16_WIP());
  return rv;
}
6  Using Arduino / Programming Questions / String "Limiter" function on: December 29, 2013, 01:12:06 am
I would like to find out if there is a function in a library such that if I were to specify the start and end position of a string, it will output that part of the string.

For example,

string= "1234567890abc"
out=string.limiter(0,4) ;    //start, stop

result: out="90abc"


string= "1234567890abc"
out=string.limiter(2,9) ;    //start, stop

result: out="4567890a"

7  Using Arduino / Programming Questions / Re: Serial.print error 0x80 on: December 15, 2013, 05:49:03 am
Null terminate your data as pauls pointed out its absence is the problem:

Code:
char c[5]={0x80,0x55,0x6d,0xb0,0x00};

Also the String only needs to reserve the number of characters needed (4), the String lib handles the +1 for the terminator.


IT WORKS!!!!! you're the best
8  Using Arduino / Programming Questions / Re: Serial.print error 0x80 on: December 15, 2013, 05:47:08 am
Quote
Still it doesnt solve the problem of the mysterious 0x80 serial.print when i view the print outs in HEX view mode. This is even after I included the 0x0A aka newline.

Any suggestions or feedbacks are greatly appreciated
I made one. You ignored it. I'm not sure why I'm bothering making another one.

Your String may include garbage because the string that it is incorporating is not NULL terminated. As a result, you can NOT rely on the String being anything other than garbage.

Fix that problem, by properly NULL terminating your char array. NOW!

Thanks PaulS i really appreciate your help. FYI, I did include the newline too. Here is a screenshot of my new result.

Still it doesnt solve the 0x80 problem as you can see
9  Using Arduino / Programming Questions / Re: Serial.print error 0x80 on: December 15, 2013, 05:38:26 am
Code:
    char c[4]={0x80,0x55,0x6d,0xb0};
    String a=c;
The assignment here makes an assumption that is NOT valid. You get away with it, in one case, by being lucky, not good. It is better to be good.

The assumption is that c is a string that can be assigned to a String. It is NOT. A string is a NULL-terminated array of chars. Your array of chars is NOT NULL -terminated.

Thanks for the feedback PaulS. I'm pretty inexperienced with coding as you can see. I'm simply coding to make things work.

Still it doesnt solve the problem of the mysterious 0x80 serial.print when i view the print outs in HEX view mode. This is even after I included the 0x0A aka newline.

Any suggestions or feedbacks are greatly appreciated
10  Using Arduino / Programming Questions / Re: Serial.print error 0x80 on: December 15, 2013, 05:05:05 am
Can you make a really minimal sketch that shows this behaviour.
I can try to replicate.
Well u see, if i were to make a minimal sketch, the program works perfectly.

this is the minimal sketch
Code:
    char c[4]={0x80,0x55,0x6d,0xb0};
    String a=c;
    Serial.print(a);
    delay(1);
    flag=0;
    for(int i=0;i<4;i++){
      for(int j=0;j<8;j++){
        if(bitRead(a[i],7-j)==0){
          flag=!flag;
        }

        bitWrite(a[i], 7-j, flag);
      }
    }
    Serial.print(a);
11  Using Arduino / Programming Questions / Re: Serial.print error 0x80 on: December 15, 2013, 04:37:13 am
thanks for your feedback rob.

the same error happens even if i use a char array. the extra 0x80 serial.print comes out of no where 
12  Using Arduino / Programming Questions / Serial.print error 0x80 on: December 15, 2013, 02:16:27 am
Dear all,

I am trying to implement non return to zero space coding for a preset data.

The algorithm works but i get an additional 0x80 when I serial.print my results out.(I used termite software HEX view mode to make sure my algorithm works) Both the original and resulted values are appended with 0x80 at the end.

When I separated the code in concern onto a blank project, it works.

Can someone help me here? I have inserted a screenshot of the 0x80 extra byte that appears after the serial.print.

Code:
String outputString="qwert",inputString = "";         // a string to hold incoming data
boolean flag=0,previous=0,stringComplete = false;  // whether the string is complete
char c[4]={0x80,0x55,0x6d,0xb0};
void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(5);
  outputString.reserve(5);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {

    flag=0;
    for(int i=0;i<5;i++){
      for(int j=0;j<8;j++){
        if(bitRead(inputString[i],7-j)==0){
          flag=!flag;
        }

        bitWrite(inputString[i], 7-j, flag);
      }
    }
    String a=c;
    Serial.print(a);
    delay(1);
    flag=0;
    for(int i=0;i<4;i++){
      for(int j=0;j<8;j++){
        if(bitRead(a[i],7-j)==0){
          flag=!flag;
        }

        bitWrite(a[i], 7-j, flag);
      }
    }
    Serial.print(a);
delay(1);
    for(int i=0;i<5;i++){
      for(int j=0;j<8;j++){
        if (j==0 && i==0)
        {
          if (bitRead(inputString[i],7)==0){
            previous=0;

            bitWrite(outputString[i],7,1);
          }
          else if(bitRead(inputString[i],7)==1){
            previous=1;
            bitWrite(outputString[i],7,0);
          }
        }
        else
          if ( bitRead(inputString[i],7-j)==previous){
            bitWrite(outputString[i],7-j,1);
            previous=bitRead(inputString[i],7-j);
          }
        else{
          previous=bitRead(inputString[i],7-j);    
          bitWrite(outputString[i],7-j,0);
        }
      }
    }
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}
13  Using Arduino / Project Guidance / Re: How to use CRC-32 on: October 24, 2013, 10:25:57 pm
How do I call CRC?

Like this:  "HEEEERE CRC, C'MERE BOY!  Who's a good validation algorithm??? YOU ARE.. yes you are..."   smiley-wink

Alright, seriously now.  The example code shows how to call it in the setup() function.  What are you having trouble with?

How many bits is a CRC?

For a CRC-32?  32.
For a CRC-16?  16.
For a CRC-7?  Let's see.... carry the 2.... add the hypotenuse... uh... 7.  smiley-grin

If i want to append CRC at the end of a 20x8 bit addresses of a EEPROM, how do I do it?

Depends entirely on your code.  Post whatcha got.

Many thanks Nick. I am able to call the CRC32 now. However, I have yet coded the part for the EEPROM. I will code it soon and post it here with the errors I'm sure to encounter. haha ...
14  Using Arduino / Project Guidance / How to use CRC-32 on: October 24, 2013, 07:33:53 pm
Hi guys, I have found the code for CRC-32 online at:
http://excamera.com/sphinx/article-crc.html
The code is as such:
Code:
#include <avr/pgmspace.h>

static PROGMEM prog_uint32_t crc_table[16] = {
    0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
    0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
    0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
    0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};

unsigned long crc_update(unsigned long crc, byte data)
{
    byte tbl_idx;
    tbl_idx = crc ^ (data >> (0 * 4));
    crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
    tbl_idx = crc ^ (data >> (1 * 4));
    crc = pgm_read_dword_near(crc_table + (tbl_idx & 0x0f)) ^ (crc >> 4);
    return crc;
}

unsigned long crc_string(char *s)
{
  unsigned long crc = ~0L;
  while (*s)
    crc = crc_update(crc, *s++);
  crc = ~crc;
  return crc;
}

void setup()
{
  Serial.println(crc_string("HELLO"), HEX);
}

void loop()
{
}

I am quite a newbie at coding ... so my question goes:

How do I call CRC?
How many bits is a CRC?
If i want to append CRC at the end of a 20x8 bit addresses of a EEPROM, how do I do it?

thanks for helping out.
15  Using Arduino / Programming Questions / Re: Serial print in I2C command on: October 24, 2013, 04:58:02 am
well, i guess that's the only way to solve the problem for now. Thanks Nick, you're great.
Pages: [1] 2