bishop707
Newbie level 1
- Joined
- Dec 16, 2014
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 14
As a test, I am trying to receive a simple array of values containing [1 2; 3 7] from matlab, store this data in flash memory then check the memory. However, I get all zeros on the serial display.
I am new to writing code for an avr controller. At this point I’m trying to isolate the problem to either hardware or software. I have verified the circuit up to this point with various blinking tutorials, but problems with the circuit could remain.
I have included the code so that any glaring mistakes can be pointed out. The code is bulky but I am trying to start with a code that works, then expand from there. When compiling this code there is only one warning, stating ‘array 'from_matlab[]' assumed to have one element.’
(1)I am using an ATmeag1284p, with a 16MHz crystal.
(2)My fuses are set for JTAGEN disabled, un-programmed, and Full Swing with slowly rising power
(3) Circuit is for +5v
(4)I am using a breadboard
(5)I am using an usb-uart converter (RS232 TTL) 5v breakout board.
(6)mcu TXD1 is to usb RX,
(7)mcu RXD0 is to usb TX
(8)Programmer used is avrisp2 and is disconnected from breadboard after programming
(9)Baud rate is 9600
(10)the flash memory is AT45DB321D, wiring has been checked, is correct
Any suggestions would be greatly appreciated. See code below.
I am new to writing code for an avr controller. At this point I’m trying to isolate the problem to either hardware or software. I have verified the circuit up to this point with various blinking tutorials, but problems with the circuit could remain.
I have included the code so that any glaring mistakes can be pointed out. The code is bulky but I am trying to start with a code that works, then expand from there. When compiling this code there is only one warning, stating ‘array 'from_matlab[]' assumed to have one element.’
(1)I am using an ATmeag1284p, with a 16MHz crystal.
(2)My fuses are set for JTAGEN disabled, un-programmed, and Full Swing with slowly rising power
(3) Circuit is for +5v
(4)I am using a breadboard
(5)I am using an usb-uart converter (RS232 TTL) 5v breakout board.
(6)mcu TXD1 is to usb RX,
(7)mcu RXD0 is to usb TX
(8)Programmer used is avrisp2 and is disconnected from breadboard after programming
(9)Baud rate is 9600
(10)the flash memory is AT45DB321D, wiring has been checked, is correct
Any suggestions would be greatly appreciated. See code below.
Code:
//MatlabToMicroTest04
//this code is to test sending data FROM matlab to 1284p, and storing in Flash memory, using uart.c by Joerg wunsch
#include <avr/io.h>
#include <util/delay.h>
#include <avr/inttypes.h>
#include <avr/uart.c>
#include <avr/uart.h>
// Yellow and Green LEDS
#define LED_D5 (1<<5) // yellow
#define LED_D4 (1<<4) // green
//create prototypes for Main
void initialize(void);
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
void initialize(void){
uart_init();
stdout = stdin = stderr = &uart_str;
}
//*************[START]******code needed for SPI, storing in Flash memory**********************
#define SETBIT(REG,BITNUM) ((void) (REG |=1<<(BITNUM)))
#define CLRBIT(REG,BITNUM) ((void) (REG &= ~(1<<(BITNUM))))
//create prototypes for SPI
void spi_int(void);
unsigned char isBusy(void);
unsigned char readStatusRegister(void);
//create variables for SPI
unsigned char junk; //variable used to interface with flash
unsigned char flashByte; //variable used to interface with flash
int AT=0; //set to fsacnf to varify sending data
//set up SPI for the flash memory********************************************
void spi_int(){
//Data Directional Register
SETBIT(DDRB, 7); //SCLK to output
CLRBIT(DDRB, 6); //MISO to input
SETBIT(DDRB, 5); //MOSI to output
SETBIT(DDRB, 4); // Chip Select
SETBIT(PORTB,4);
//Set up SPCR
CLRBIT(SPCR, 7); //disable interrupts
SETBIT(SPCR, 6); //SPE - enable
CLRBIT(SPCR, 5); //DORD msb (most sig bit)first
SETBIT(SPCR, 4); //MSTR - set uc (micro) as master
CLRBIT(SPCR, 3); //CLOCK PHASE/POLARITY
CLRBIT(SPCR, 2); //Mode 0
CLRBIT(SPCR, 1); //SPCR=SPR1<<0, for fck/16
SETBIT(SPCR, 0); //SPCR=SPR0<<1, for fck/16
//Set up SPSR
SPSR=0x00; //overclock bit; speed 1/16
} //end spi_int
//***************************************************************************
//Read Status Register
unsigned char readStatusRegister() {
CLRBIT(PORTB, 4); //Select Chip
SPDR = 0xD7; //this is the 'read status' command
while(!(SPSR & (1<<SPIF))); //polling the SPI Interrupt Flag (SPIF)
junk = SPDR; //read Data Register
SPDR = junk; //causes 'junk' to be written
while(!(SPSR & (1<<SPIF))); //polling the SPI Interrupt Flag (SPIF)
flashByte = SPDR;
SETBIT(PORTB, 4); // de-select chip
return flashByte;
}// end readStatusRegister
//***************************************************************************
// is flash memory busy?
// The seventh bit of the Status Register is "not busy"
unsigned char isBusy(){
return !(readStatusRegister()>>7);
}//end isBusy
//***************************************************************************
//variables for buffer
unsigned int byte = 0; //write to buffer, byte starting at '0' within the buffer
//variables for memory
unsigned int page = 0; //write to memory, page starting at '0' within the memory
//variables used to store in-coming matlab values
unsigned int n = 4; //this is the length of from_matlab[]
unsigned int from_matlab[4]; //value coming from matlab
int main(void)
{
//set up the ports
DDRA=0x01;
DDRB=0xa8;
DDRC=0x00;
DDRD |= 0b00110000; //data directional register for portD for LED D4=green and D5=yellow
spi_int(); //initialize SPI for flash memory
initialize(); //initialize uart
//*************************blink to verify initialized
PORTD |= (LED_D5);
_delay_us(500000);
PORTD &= ~(LED_D5);
//*************************
while(1)
{
AT=fscanf(stdin, "%d", &from_matlab[1]);//received value from matlab
if(AT==1){
//*************************blink to verify received from matlab
PORTD |= (LED_D4);
_delay_us(50);
PORTD &= ~(LED_D4);
//*************************
}
while(isBusy());
//************[START]********send to 1284p buffer*****************************************
//write to Buffer in 1284p
// Write 'n' bytes to buffer
// opcode 84H (87H for buffer 2)
// 3 addr bytes: 10 'Don't Care', 10 buffer addr bytes
// then can send data
unsigned char opcode_Buf = 0x00;
opcode_Buf = 0x84; //84H is buffer 1
unsigned char message_Buf[4] =
{opcode_Buf, // opcode
0x00, // 'Don't Care' bytes
(char) (byte>>8), // [2 'Don't Care bytes; BA9-BA8
(char) (byte)}; // [BA7 - BA0]
CLRBIT(PORTB,4); // Select Chip
//***starts sending BYTES over SPI***
for (int k=0; k<4; k++){
SPDR = message_Buf[k];
while(!(SPSR & (1<<SPIF))); //causes 'wait until complete'
junk = SPDR; // prompts 'read Data Register'
}
//***Now, start sending DATA over SPI***
for (int k=0; k<n; k++){
SPDR = from_matlab[k];//assuming that the RIGHTSIDE of SPDR is in-coming data from matlab
while(!(SPSR & (1<<SPIF))); //causes 'wait until complete'
junk = SPDR; // prompts 'read Data Register'
}
SETBIT(PORTB, 4); //de-select chip
//************[end]********send to 1284p buffer*******************************************
while(isBusy());
//************[START]********send to 1284p from buffer to main memory*********************
//write main memory for buffer (with or without built-in erase)
// send data from matlab, from 1284p buffer to 1284p main Memory
// Built-in erase: opcode 83H (86H for buffer 2)
// No erase: opcode 88H (89H for buffer 2)
// 3 addr; 1 'Don't Care' byte, 13 page address, 10 'Don't Care' bytes
unsigned char opcode_Main = 0x00; //declare and initially assign opcode
opcode_Main = 0x83;//buffer 1, built-in erase
unsigned char message_Main[4] =
{opcode_Main, // opcode
(char) (page>>6), // ['Don't Care' byte, PA12-PA6]
(char) (page<<2), // [PA5-PA0 2DC]
0x00}; // 'Don't Care'
CLRBIT(PORTB, 4); // select chip
//start sending BYTES over SPI
for(int k=0; k<4; k++){
SPDR = message_Main[k];
while(!(SPSR & (1<<SPIF))); //causes 'wait until complete'
junk = SPDR; // prompts 'read Data Register'
}
SETBIT(PORTB, 4); //de-select chip
//************[END]********send to 1284p from buffer to main memory***********************
}//end while(1)
} //end 'main'