expected declaration or statement at end of input

Go To Last Post
37 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,am tryind to compile the code below in winavr programmer's notepad but i get the error:

 

main.c: In function 'write':
main.c:817: error: expected declaration or statement at end of input
make.exe: *** [main.o] Error 1

Any idear how to solve? i have been trying to solve it for the last two days witout sucess and each time i try to adit something i get more errors.

The error is on the last line in the program with '}'

 

#include <avr/io.h>
#include <avr/iom32.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <ctype.h>
#include <avr/pgmspace.h>
#include <math.h>
#include <stdlib.h>
#include <lcd.h>
#include "i2cmaster.h"
#include "ds1302.h"
#define MAX_STRING_SIZE 100 //defining the maximum size of the dataString
#define DI 6 // Port B bit 6 (pin7): data in (data from MMC)
#define DT 5 // Port B bit 5 (pin6): data out (data to MMC)
#define CLK 7 // Port B bit 7 (pin8): clock
#define CS 4 // Port B bit 4 (pin5): chip select for MMC
#define RED_LED_ON PORTC |=(1<<6)
#define RED_LED_OFF PORTC &=~(1<<6)
#define BAUD 9600 // define baud
#define BAUDRATE 0x67 // set baud rate value for UBRR
//#define FOSC 16000000
char buffer[10];
char buffer2[25];
char myarray[30];
int s,button_filter;
int butflag=0;
static unsigned char buffer3[512];
static unsigned char buffer4[512];
//data string where data is collected before sending to the card
void ShowMainMenu(void);
void SetTime(void);
volatile unsigned char dataString[MAX_STRING_SIZE];
char chars[512];
void initADC (void);
void initialize_ports (void);
unsigned int sample_voltage(void);
void Sound_alarm (void);
void Turn_on_LED (void);
void Turn_off_LED (void);
void Reset_Alarm (void);
void Readvoltage (unsigned char channel);
void Send_sms (void);
char SPI_sendchar(char chr);
char Command(char cmd, uint16_t ArgH, uint16_t ArgL, char crc );
int write(unsigned char *buffer);
void Read (void);
unsigned char UART_receive( void );
void UART_transmit( unsigned char data );
void init_UART (void);
void blinkRedLED(void);
void ini_SPI(void);
void ini_SPI(void);
void send_UART_string(char * byte);
void get_command(void);
void ini_SD(void);
// global variables
unsigned int Vmin; // value of voltage minimum
unsigned int Vmax; // value of voltage maximum
unsigned int counter_value; // value of count
int volt;
double voltag;
int timer;
char chars[512];
char sms_read[]={'A','T','+','C','M','G','R','='};
char voltage[8];
uint16_t theTenBitResult;
unsigned long a;
unsigned char i=0;
unsigned char j,k,m;
unsigned char proba;
unsigned char temp_array [64];
double tempcalc,tempcalc2;
int index1;
int colcount=0;
int retry,amount;
int t;
int timer;
char sms_send[]={'A','T','+','C','M','G','S','=','"','0','7','1','7','7','2','6','3','7','6','"'};
char sms_send_number[30];
char message[160];
char smsind[6];
char sms_number[16];
char sms_read_comm[13]; //SPp
int l=0;
int flag=0;
int t=0;
char ok[2]={'O','K'};
//INITIALIZING GLOBAL VARIABLES
int buffer_size=200;
int x,y;
char data_buffer[200];
int enter_count=0;
int com_start, com_end;
char command[20];
void send_sms(char *message);
int compare(char command[], char command_saved[]);
char junk_check=0;//variable to check authenticity of message
char error[5]={'E','R','R','O','R'};
float v;
uint8_t PREV_PINB=((1<<PB2)|(1<<PB1)|(1<<PB0));
uint8_t GetKeyStatus(uint8_t key)// function to test current status of keys
{
return (!(PINB & (1<<key)));
}
uint8_t GetPrevKeyStatus(uint8_t key)
{
return (!(PREV_PINB & (1<<key)));
}
void Wait(void)
{
uint8_t i;
for(i=0;i<20;i++)
_delay_loop_2(0);
}
void fillram(void)
{
uint16_t i;
Readvoltage(0);
for(j=0;j<7; j++) dataString[i++] =voltage[j];
for (i=0;i<512;i++)
{
buffer3[i] =voltage[j];
}
}
void main(void)
{
void init_sytem(void);
ADCSRA |= 1<<ADSC; //ADC Start Conversion
_delay_ms(10);
lcd_gotoxy(1,0);
lcd_puts("welcome");
_delay_ms(100);
//Enable Pull ups on keys
PORTB|=((1<<PB2)|(1<<PB1)|(1<<PB0));
//Clear CH bit of RTC
#define CH 7
uint8_t temp;
ds1302Read(0x00,&temp);
//Clear CH Bit
temp&=(~(1<<CH));
ds1302Write(0x00,temp);
//Set 12 Hour Mode
ds1302Read(0x02,&temp);
//Set 12Hour BIT
temp|=(0b01000000);
//Write Back to ds1302
ds1302Write(0x02,temp);
char Time[12]; //hh:mm:ss AM/PM
//Now Read and format time
uint8_t data;
Vmin=216;//state_machine();
Vmax=264;//state_machine();
counter_value=5; //State_machine();
while(1)
{
char VOLTAGE[]={'V','O','L','T','A','G','E'};
int c;
ds1302Read(0x00,&data);
Time[8]='\0';
Time[7]=48+(data & 0b00001111)/ 0x30;
Time[6]=48+((data & 0b01110000)>>4)/ 0x30;
Time[5]=':';
ds1302Read(0x01,&data);
Time[4]=48+(data & 0b00001111)/ 0x30;
Time[3]=48+((data & 0b01110000)>>4)/ 0x30;
Time[2]=':';
ds1302Read(0x02,&data);
Time[1]=48+(data & 0b00001111)/ 0x30;
Time[0]=48+((data & 0b00010000)>>4)/ 0x30;
lcd_clrscr();
lcd_gotoxy(11,1);
lcd_puts(Time);
init_UART();
send_UART_string(Time);
_delay_ms(1000);
//AM/PM
if(data & 0b00100000)
{
lcd_gotoxy(11,1);
lcd_puts("pm");
}
else
{
lcd_gotoxy(11,1);
lcd_puts("am");
}
fillram();
lcd_gotoxy(2,0);
lcd_puts(voltage);
_delay_ms(100);
c=0;
v=volt;
if (v>=Vmin && v<=Vmax)// check whether voltage is withing range
{
c=0;// clear counter
}
else
{
if (c==counter_value)// check if counter value has been reached
{
send_sms("ENGINEER THE VOLTAGE IS ABOVE RANGE");
PORTC |= 1<<5; //set_alarm_on();
c=0;//clear counter
}
else
{
c++;// increment counter
}
}
//error=write(buffer3); // write to sd card
//if(error) blinkRedLED();
printf(buffer2, "%.3f Volts\r\n", volt);
//send_sms(buffer2)
get_command();
if(compare(command, VOLTAGE))
{
PORTC |= 1<<6;
send_sms(buffer2);
PORTC &=~(1<<6);
}
//Wait Some time and keep testing key input
uint8_t i;
for(i=0;i<20;i++)
{
if(GetKeyStatus(2))
{
//Go To Main Menu
ShowMainMenu();
lcd_gotoxy(1,0);
lcd_puts("whatis wrong");
_delay_ms(1000);
_delay_loop_2(0);
_delay_loop_2(0);
_delay_loop_2(0);
}
else
{
break;
}
}
}// END WHILE
return;
}//END MAIN
// initialize ports and pins
void initialize_ports()
{
DDRA=0x00;
PORTA=0X00;
DDRB=0XB8;
PORTB=0X00;
DDRC=0x60;
PORTC=0X00;
DDRD= 0x00;
}
void ShowMainMenu(void)
{
//The Main Menu
char *menu_items[]={ "Set Time","Set voltage","Quit"};
uint8_t menu_count=3;
uint8_t selected=0;
_delay_loop_2(0);
_delay_loop_2(0);
while(1)
{
lcd_clrscr();
lcd_gotoxy(1,0);
lcd_puts("main menu");
lcd_gotoxy(2,1);
lcd_puts(menu_items[selected]);
lcd_gotoxy(0,1);
lcd_puts("<");
lcd_gotoxy(15,1);
lcd_puts(">");
if(GetKeyStatus(1))
{
//Left Key(No 1) is pressed
//Check that it was not pressed previously
if(!GetPrevKeyStatus(1))
{
if(selected !=0)
selected--;}
}
if(GetKeyStatus(0))
{//Right Key(No 0) is pressed
//Check that it was not pressed previously
if(!GetPrevKeyStatus(0))
{
if(selected !=(menu_count-1))
selected++;
}
}
if(GetKeyStatus(2))
{
//Enter Key Pressed
//Check that it was not pressed previously
if(!GetPrevKeyStatus(2))
{
//Call Appropriate Function
switch (selected)
{
case 0:
SetTime();
break;
case 1:
//Setvoltage();
break;
case 2:
return;//Quit
}
}
}
PREV_PINB=PINB;
_delay_loop_2(5000);
}
}
void SetTime()
{
uint8_t hr,min,sec,am_pm,temp;
//Read the Second Register
ds1302Read(0x00,&temp);
sec=(((temp & 0b01110000)>>4)*10)+(temp & 0b00001111);
//Read the Minute Register
ds1302Read(0x01,&temp);
min=(((temp & 0b01110000)>>4)*10)+(temp & 0b00001111);
//Read the Hour Register
ds1302Read(0x02,&temp);
hr=(((temp & 0b00010000)>>4)*10)+(temp & 0b00001111);
am_pm=(temp & 0b00100000)>>4;
//If Hour Register is 0 make it 12, as 00:00:00 invalid time
if(hr==0) hr=12;
uint8_t sel=0;
while(1)
{
lcd_clrscr();
lcd_gotoxy(0,0);
lcd_puts("00:00:00 <OK>");
lcd_gotoxy(0,0);
lcd_puts("hr:00:00");
lcd_gotoxy(3,0);
lcd_puts("00:min:00");
lcd_gotoxy(6,0);
lcd_puts("00:00:sec");
if(am_pm)
{
lcd_gotoxy(9,0);
lcd_puts("PM");
}
else
{
lcd_gotoxy(9,0);
lcd_puts("AM");
}
//Draw Pointer
lcd_gotoxy(sel*3,1);
lcd_puts("^^");
//Input Up key
if(GetKeyStatus(1))
{
if(!GetPrevKeyStatus(1))
{
if(sel==0)
{
//Hour
if(hr==12)
{
hr=1;
}
else
{
hr++;
}
}
if(sel==1)
{//Min
if(min==59)
{min=0;
}
else
{min++;
}
}
if(sel==2)
{
//Sec
if(sec==59)
{
sec=0;
}
else
{
sec++;
}
}
if(sel==3)
{//AM-PM
if(am_pm==0)
{
am_pm=1;
}
else
{
am_pm=0;
}
}
if(sel == 4)
{
//OK
break;
}
}
}//Input Down
if(GetKeyStatus(0))
{
if(!GetPrevKeyStatus(0))
{
if(sel==0)
{ //Hour
if(hr==1)
{
hr=12;
}
else
{hr--;
}
}
if(sel==1)
{
//Min
if(min==0)
{
min=59;
}
else
{
min--;
}
}
if(sel==2)
{ //Sec
if(sec==0)
{
sec=59;
}
else
{
sec--;
}
}
if(sel==3)
{
 //AM-PM
if(am_pm==0)
{
am_pm=1;
}
else
{
am_pm=0;
}
}
if(sel == 4)
{
//OK
break;
}
}
}
if(GetKeyStatus(2))
{
if(!GetPrevKeyStatus(2))
{
//Change Selection
if(sel==4)
sel=0;
else
sel++;
}
}
PREV_PINB=PINB;
_delay_loop_2(30000);
}
//Now write time back to RTC Module
temp=((sec/10)<<4)|(sec%10);
ds1302Write(0x00,temp);
temp=((min/10)<<4)|(min%10);
ds1302Write(0x01,temp);
temp=((hr/10)<<4)|(hr%10);
temp|=0b01000000; //12 Hr Mode
if(am_pm)
{
temp|=0b00100000;
}
ds1302Write(0x02,temp);
lcd_clrscr();
lcd_gotoxy(0,0);
lcd_puts("mesage!");
lcd_gotoxy(0,1);
lcd_puts("main time set");
uint8_t i;
for(i=0;i<10;i++)
_delay_loop_2(0);
}
void initADC( void) // initialize adc of the microcontroller
{
ADMUX = 0X00; //Select channel 0
ADCSRA = 0xC3; //Enable ADC & start 1st
//dummy conversion
//Set ADC module prescalar
//to 8
//critical for accurate ADC results
ADCSRA |= 1<<ADIE;//ADC INTERUPT ENABLE
while (!(ADCSRA & 0x10)); //Check if conversation is ready
ADCSRA |= 0x10; //Clear conv rdy flag-set the bit
}
unsigned int sample_voltage(void)
{
uint8_t theLowADC = ADCL;
uint16_t voltage_sample = ADCH<<8 | theLowADC;
voltag= voltage_sample;
return voltag;
}
void Readvoltage (unsigned char channel)
{
int value;
ADMUX=0x00;
value =sample_voltage();
volt = (float)(value * 5.0)/ 1024.0;
value = (unsigned int)(volt * 60);
voltage[6] = 'V'; //V for voltage
voltage[5] = ' ';
voltage[4] = (value % 10) | 0x30;
value = value / 10;
voltage[3] = (value % 10) | 0x30;
value = value / 10;
voltage[2] = (value % 10) | 0x30;
voltage[1] = '.';
value = value / 10;
voltage[0] = value | 0x30;
}
// function to initialize UART
void init_UART (void)
{
UBRRH = (BAUDRATE>>8); // shift the register right by 8 bits
UBRRL = BAUDRATE; // set baud rate
UCSRC=(1<<URSEL)|(0<<UMSEL)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
UCSRB=(1<<RXEN)|(1<<TXEN)|(1<<RXCIE); //Enable Transmitter and Receiver and Interrupt on receive complete
sei();// enable global interrupts
}
// CLEAR THE MESSAGE BUFFER FOR NEXT STRING OF CHARACTERS
void clear_buffer(void)
{
for(x=0; x<buffer_size; x++)data_buffer[x]=0; //initialising the data array to null
y=0;// reset the buffer counter
x=0;
for(x=0;x<sizeof(command);x++)command[x]=0;
}
// PRINT A SINGLE CHARACTER TO THE USART.
void send_UART_char(char txt)
{
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR = txt; // send the character to computer.
}
//PRINT A STRING TO USART AND TERMINATE WITH A CARRIAGE RETURN
void send_UART_string(char * byte)
{
for (;*byte;++byte) send_UART_char(*byte);
send_UART_char(13);
}
void send_UART_string2(char * byte)//function to finish sms sending
{
for (;*byte;++byte) send_UART_char(*byte);
send_UART_char(26);// control-z combination to complete send sms
}
char get_char(void)
{
char temp;
temp=UDR;
return temp;
}
void fill_buffer(void)
{
data_buffer[y]=get_char();
y++;
if(y==200)y=0;
}
ISR(USART_RXC_vect)
{
fill_buffer();
}
void get_command(void) // function to extract the command from the sms
{
int Getcommand1=0;
int Getcommand=0;
if(data_buffer[Getcommand]!='*')
{
Getcommand++;
}
Getcommand++;
if((data_buffer[Getcommand]!='#'))
{
command[Getcommand1]=data_buffer[Getcommand];
Getcommand++;
Getcommand1++;
}
}
bool Get_AT_Response(char * response)
{
_delay_ms(2000);
if (strstr_P(data_buffer, response)) //If string is found, would return pointer value so becomes true
return 1;
else
return 0;
}
void Put_ATE0(void) // Sends echo command off
{
retry=1;
while (1)
{
clear_buffer();
send_UART_string("ATE0\r");
_delay_ms(500);
if (Get_AT_Response(PSTR("OK")))
break;
}
}
void Put_AT_CMGF(void) // Set to sms mode
{
while (1)
{
clear_buffer();
send_UART_string("AT+CMGF=1\r");
_delay_ms(500);
if (Get_AT_Response(PSTR("OK")))
break;
}
}
void Put_AT_CNMI(void) // set sms to be received automatically
{
while (1)
{
clear_buffer();
send_UART_string("AT+CNMI=2,2,0,0,0");
_delay_ms(1000);
if (Get_AT_Response(PSTR("OK")))
break;
}
}
void Put_AT_CMGS(char *number,char *message) // sends sms message
{
while (1)
{
_delay_ms(2000);
clear_buffer();
send_UART_string(number);
_delay_ms(2000);
if (Get_AT_Response(PSTR(">")))
{
send_UART_string2(message);
_delay_ms(5000);
break;
}
}
}
void init_sytem(void)
{initialize_ports();
initADC();
ini_SPI();
ini_SD();
init_UART();
i2c_init();
//ini_RTC();
Put_ATE0();
Put_AT_CMGF();
Put_AT_CMGF();
Put_AT_CNMI();
Put_AT_CNMI();
clear_buffer();
send_sms("VOLTAGE MORNITORING AND LOGGING INITIATED SUCESSFULLY, EVERYTHING OK!");
_delay_ms(1000);
}
void blinkRedLED(void)
{
//blink red LED , if error
RED_LED_ON;
_delay_ms(400);
RED_LED_OFF;
_delay_ms(400);
}
void send_sms(char *message)
{
PORTB |= 1<<0;
int k=sizeof(sms_send);
int sms_number_length=0;
//Put_AT_CMGF();
for (sms_number_length=0;sms_number_length<16;sms_number_length++)
if (sms_number[sms_number_length]==0)
break;
memcpy(sms_send_number,sms_send,k); //send SMS header
memcpy(sms_send_number+k,sms_number,((sizeof(char))*sms_number_length)); //GSM number
//SPP -
k+=12;
sms_send_number[k++]='"'; //collons
sms_send_number[k]='\0'; //terminate string
clear_buffer();
Put_AT_CMGS(sms_send_number,message);
}
int compare(char command[], char command_saved[])
{
int x=0;
for(x=0;x<sizeof(command);x++)
{
if(command[x]!=command_saved[x])
return 0;
}
return 1;
}
//SPI Initialization:
void ini_SPI(void)
{
DDRB &= ~(_BV(DI)); //input
DDRB |= _BV(CLK); //outputs
DDRB |= _BV(DT); //outputs
DDRB |= _BV(CS); //outputs
SPCR |= _BV(SPE); //SPI enable
SPCR |= _BV(MSTR); //Master SPI mode
SPCR |=(_BV(SPR1)); //fosc/64
SPCR &= ~ _BV(SPR0); //fosc/64
SPSR &= ~(_BV(SPI2X)); //speed is not doubled
PORTB &= ~(_BV(CS));
//Enable CS pin for the SD card
}
//Initialization card:
void ini_SD(void)
{
char i;
PORTB |= _BV(CS); //disable CS
for(i=0; i < 10; i++)
SPI_sendchar(0xFF); // Send 10 * 8 = 80 clock pulses 400 kHz
PORTB &= ~(_BV(CS)); //enable CS
for(i=0; i < 2; i++)
SPI_sendchar(0xFF); // Send 2 * 8 = 16 clock pulses 400 kHz
Command(0x40,0,0,0x95); // reset
idle_no:
if (Command(0x41,0,0,0xFF) !=0)
goto idle_no; //idle = L?
SPCR &= ~(_BV(SPR0)); //fosc/4
}
//Function to send a command frame Command:
char Command(char cmd, uint16_t ArgH, uint16_t ArgL, char crc )
{
SPI_sendchar(0xFF);
SPI_sendchar(cmd);
SPI_sendchar((uint8_t)(ArgH >> 8));
SPI_sendchar((uint8_t)ArgH);
SPI_sendchar((uint8_t)(ArgL >> 8));
SPI_sendchar((uint8_t)ArgL);
SPI_sendchar(crc);
SPI_sendchar(0xFF);
return SPI_sendchar(0xFF); // Returns the last byte received
}
//Functions for sending and receiving one byte through SPI:
char SPI_sendchar(char chr)
{
char receivedchar = 0;
SPDR = chr;
while(!(SPSR & (1<<SPIF)));
receivedchar = SPDR;
return (receivedchar);
}
//Writing to the card:
//The function returns 1 if an error occurs else returns 0 if successful
int write(unsigned char *buffer)
{
uint8_t wbr;
//Set write mode 512 bytes
if (Command(0x58,0,512,0xFF) !=0)
{
//Determine value of the response byte 0 = no errors
return 1;//return value 1 = error }
SPI_sendchar(0xFF);
SPI_sendchar(0xFF);
SPI_sendchar(0xFE);
//recommended by posting a terminator sequence [2]
//write data from chars [512] tab
uint16_t ix;
char r1 = Command(0x58,0,512,0xFF);
for (ix = 0; ix < 50000; ix++) // che k for valid data lenght
{
if (r1 == (char)0x00)
break;
r1 = SPI_sendchar(0xFF);
}
if (r1 != (char)0x00)
{
return 1;
//return value 1 = error
}
//recommended by the control loop [2]
SPI_sendchar(0xFF);
SPI_sendchar(0xFF);
wbr = SPI_sendchar(0xFF);
//write block response and testing error
wbr &= 0x1F;
//zeroing top three indeterminate bits 0b.0001.1111
if (wbr != 0x05)
{
 // 0x05 = 0b.0000.0101
 //write error or CRC error
return 1;
}
while(SPI_sendchar(0xFF) != (char)0xFF);
//wait for the completion of a write operation to the card
return 0;
}

 

Last Edited: Thu. Jul 7, 2016 - 01:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It means the compiler was expecting more to follow - either statement(s) or declaration(s) - when it reached the end of the source file.

 

Or, in other words, the file ends in the middle of something.

 

Most likely, you have mis-matched braces somewhere ...

 

A decent programmer's editor (including Atmel Studio) will have facilities for showing brace matching - or you can just do it by hand with a pencil ...

 

Note that the error could originate in a #included file ...

Last Edited: Thu. Jul 7, 2016 - 11:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to get solutions online and what i get is  that the error occurs when},{,(,) or ;  } was missed somewhere in the code, but i cant trace where

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please tell me that you don't really write all your code flush left!

 

Anyway Linux (and probably Windows somewhere) have a tool called indent available that will apply sensible indenting to code like this. I just use "indent -kr file.c" and got this (attached). As you'll see, towards the end it finishes like this:

 

//Writing to the card:
//The function returns 1 if an error occurs else returns 0 if successful
int write(unsigned char *buffer)
{
    uint8_t wbr;
//Set write mode 512 bytes
    if (Command(0x58, 0, 512, 0xFF) != 0) {
//Determine value of the response byte 0 = no errors
	return 1;		//return value 1 = error }
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFE);
//recommended by posting a terminator sequence [2]
//write data from chars [512] tab
	uint16_t ix;
	char r1 = Command(0x58, 0, 512, 0xFF);
	for (ix = 0; ix < 50000; ix++)	// che k for valid data lenght
	{
	    if (r1 == (char) 0x00)
		break;
	    r1 = SPI_sendchar(0xFF);
	}
	if (r1 != (char) 0x00) {
	    return 1;
//return value 1 = error
	}
//recommended by the control loop [2]
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	wbr = SPI_sendchar(0xFF);
//write block response and testing error
	wbr &= 0x1F;
//zeroing top three indeterminate bits 0b.0001.1111
	if (wbr != 0x05) {
	    // 0x05 = 0b.0000.0101
	    //write error or CRC error
	    return 1;
	}
	while (SPI_sendchar(0xFF) != (char) 0xFF);
//wait for the completion of a write operation to the card
	return 0;
    }

That function ends before control has returned to a '}' in the left margin.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Am using windows10,how can i get the Indent and how do i intall it .The code you attached gave the following errors

 

main.c: In function 'main':
main.c:157: error: expected ')' before 'b01000000'
main.c:171: error: expected ')' before 'b00001111'
main.c:172: error: expected ')' before 'b01110000'
main.c:175: error: expected ')' before 'b00001111'
main.c:176: error: expected ')' before 'b01110000'
main.c:179: error: expected ')' before 'b00001111'
main.c:180: error: expected ')' before 'b00010000'
main.c:188: error: expected ')' before 'b00100000'
main.c: In function 'SetTime':
main.c:316: error: expected ')' before 'b01110000'
main.c:316: error: expected ')' before 'b00001111'
main.c:319: error: expected ')' before 'b01110000'
main.c:319: error: expected ')' before 'b00001111'
main.c:322: error: expected ')' before 'b00010000'
main.c:322: error: expected ')' before 'b00001111'
main.c:323: error: expected ')' before 'b00100000'
main.c:443: error: expected ';' before 'b01000000'
main.c:445: error: expected ';' before 'b00100000'
main.c: In function 'write':
main.c:792: error: expected declaration or statement at end of input
make.exe: *** [main.o] Error 1

Last Edited: Thu. Jul 7, 2016 - 12:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you look at line 157 of the file provided by clawson:

    temp |= (0 b01000000);

you can see that there is an unnecessary 0 before the b.

Several of the errors relate to similar problems.

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forgot to say that indent only knows "standard" C and because the 0b prefix is a GNU extension it mishandles it. So you either need to fix any use of 0bnnnnnn after using indent or just avoid using 0b in the first place (this is why God invented 0x - which is standard).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why is this in the projects forum?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:

Why is this in the projects forum?

It isn't any more ;-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The crrly braket turns red when i click at it unlike the others which turn blue.Could the problem be there and how do i remove it?The solution you gave still doest help,the error still occurs

 

//Writing to the card:
//The function returns 1 if an error occurs else returns 0 if successful
int write(unsigned char *buffer)
{
    uint8_t wbr;
//Set write mode 512 bytes
    if (Command(0x58, 0, 512, 0xFF) != 0) {
//Determine value of the response byte 0 = no errors
	return 1;		//return value 1 = error }
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFE);
//recommended by posting a terminator sequence [2]
//write data from chars [512] tab
	uint16_t ix;
	char r1 = Command(0x58, 0, 512, 0xFF);
	for (ix = 0; ix < 50000; ix++)	// che k for valid data lenght
	{
	    if (r1 == (char) 0x00)
		break;
	    r1 = SPI_sendchar(0xFF);
	}
	if (r1 != (char) 0x00) {
	    return 1;
//return value 1 = error
	}
//recommended by the control loop [2]
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	wbr = SPI_sendchar(0xFF);
//write block response and testing error
	wbr &= 0x1F;
//zeroing top three indeterminate bits 0b.0001.1111
	if (wbr != 0x05) {
	    // 0x05 = 0b.0000.0101
	    //write error or CRC error
	    return 1;
	}
	while (SPI_sendchar(0xFF) != (char) 0xFF);
//wait for the completion of a write operation to the card
	return 0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The whole point of indenting is so that if you see:

{
    {
        {
            {
                
            }
        }
    }
}

you know that code will compile OK but if you see:

{
    {
        {
            {
                
            }
        }
}

or

{
    {
        {
            {
                
            }
        }
    }

you know it won't. You function there falls into the latter category. It starts here

int write(unsigned char *buffer)
{

then indents once here:

    if (Command(0x58, 0, 512, 0xFF) != 0) {

while:

	for (ix = 0; ix < 50000; ix++)	// che k for valid data lenght
	{
	}

indents then outdents that is fine as these brackets are balanced. The same here:

	if (r1 != (char) 0x00) {
	}

and here:

	if (wbr != 0x05) {
	}

but at this stage the code is still indented by two levels (the start of the function and the "if (Command..>") but it ends with:

	return 0;
    }

But that return and } are NOT at the end of the function. The } will just be taken as the closing of "if (Command.." not the final end of the function.

 

The very fact that the last } is indented by 4 characters is telling you "you can't stop yet, you haven't unwound enough levels of } yet".

 

This is the whole point of using consistent indentation in your code. Presumably you meant for the "if (Command.." statement block to end at some point earlier in the function?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks,i get your point  i tried indenting but its not working with my compiler.I'll be so greatfull if you attached the indented copy or the screen shots.When i indent the compiler only moves the code away from the margin,it does not display as you've ilustrated above.please let me know where exactly in the program to make the changes.Sorry for the too many questions

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sStephen wrote:
i tried indenting but its not working with my compiler
What editor are you using?

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sStephen wrote:
please let me know where exactly in the program to make the changes.

There is no way that we can tell that.  Do you understand that in C, when you open a block with { then there must be a matching } to close the block?

 

Start over with your empty function and build:

 

 

int write(unsigned char *buffer)
{
}

Then add one block of code at a time, and rebuild each time.  Take care that all statements and blocks are properly terminated.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Got it,the error is at  "return 1;" and at the end of that line is the missing closing brace. When i move that } to the next line before "SPI_sendchar" the error disapears.But there is another problem;am using  I2C library and each time i include the library,the clossing brace disappears and the error repeatsts.I hve tried this severaly but the error keeps repeating once i include any I2C library.Any help there

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looks like the space character after the period at the end of the sentence is disappearing also.

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

After trying several i2c libraries the error occurs no more but for the i2c library am using i get the foolowing errors in the library

 

In file included from main.c:13:
c:/winavr-20100110/lib/gcc/../../avr/include/SoftI2CMaster.h:65: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'I2cMasterBase'
c:/winavr-20100110/lib/gcc/../../avr/include/SoftI2CMaster.h:94: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SoftI2cMaster'
c:/winavr-20100110/lib/gcc/../../avr/include/SoftI2CMaster.h:114: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'TwiMaster'

 

 

the errors are at line 65,94 and 114

any help

 

............................................................................

/* Arduino I2cMaster Library
 * Copyright (C) 2010 by William Greiman
 *
 * This file is part of the Arduino I2cMaster Library
 *
 * This Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This Library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Arduino I2cMaster Library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#ifndef I2C_MASTER_H
#define I2C_MASTER_H
/**
 * \file
 * \brief Software I2C and hardware TWI library
 */
#if ARDUINO < 100
#include <WProgram.h>
#else  // ARDUINO
#include <Arduino.h>
#endif  // ARDUINO
/** hardware I2C clock in Hz */
uint32_t const F_TWI = 400000L;

/** Delay used for software I2C */
uint8_t const I2C_DELAY_USEC = 4;

/** Bit to or with address for read start and read restart */
uint8_t const I2C_READ = 1;

/** Bit to or with address for write start and write restart */
uint8_t const I2C_WRITE = 0;
//------------------------------------------------------------------------------
// Status codes in TWSR - names are from Atmel TWSR.h with TWSR_ added

/** start condition transmitted */
uint8_t const TWSR_START = 0x08;

/** repeated start condition transmitted */
uint8_t const TWSR_REP_START = 0x10;

/** slave address plus write bit transmitted, ACK received */
uint8_t const TWSR_MTX_ADR_ACK = 0x18;

/** data transmitted, ACK received */
uint8_t const TWSR_MTX_DATA_ACK = 0x28;

/** slave address plus read bit transmitted, ACK received */
uint8_t const TWSR_MRX_ADR_ACK = 0x40;

//------------------------------------------------------------------------------
/**
 * \class I2cMasterBase
 * \brief Base class for SoftI2cMaster and TwiMaster
 */
class I2cMasterBase {
 public:
  /** Read a byte
   * \param[in] last send Ack if last is false else Nak to terminate read
   * \return byte read from I2C bus
   */
  virtual uint8_t read(uint8_t last) = 0;
  /** Send new address and read/write bit without sending a stop.
   * \param[in] addressRW i2c address with read/write bit
   * \return true for success false for failure
   */
  virtual bool restart(uint8_t addressRW) = 0;
  /** Issue a start condition
   * \param[in] addressRW i2c address with read/write bit
   * \return true for success false for failure
   */
  virtual bool start(uint8_t addressRW) = 0;
  /** Issue a stop condition. */
  virtual void stop(void) = 0;
  /** Write a byte
   * \param[in] data byte to write
   * \return true for Ack or false for Nak */
  virtual bool write(uint8_t data) = 0;
};
//------------------------------------------------------------------------------
/**
 * \class SoftI2cMaster
 * \brief Software I2C master class
 */
class SoftI2cMaster : public I2cMasterBase {
 public:
  SoftI2cMaster(uint8_t sdaPin, uint8_t sclPin);
  uint8_t read(uint8_t last);
  bool restart(uint8_t addressRW);
  bool start(uint8_t addressRW);
  void stop(void);
  bool write(uint8_t b);
 private:
  SoftI2cMaster() {}
  uint8_t sdaPin_;
  uint8_t sclPin_;
};
//------------------------------------------------------------------------------
/**
 * \class TwiMaster
 * \brief Hardware I2C master class
 *
 * Uses ATmega TWI hardware port
 */
class TwiMaster : public I2cMasterBase
 {
 public:
  explicit TwiMaster(bool enablePullup);
  uint8_t read(uint8_t last);
  bool restart(uint8_t addressRW);
  bool start(uint8_t addressRW);
  /** \return status from last TWI command - useful for library debug */
  uint8_t status(void) {return status_;}
  void stop(void);
  bool write(uint8_t data);
 private:
  TwiMaster() {}
  uint8_t status_;
  void execCmd(uint8_t cmdReg);
};
#endif  // I2C_MASTER_H

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Realise you can't just choose any random library and expect it to work. Here you've chosen what looks to be a c++ library for Arduino. Without a bit of work on your part, it is not going to work.
The code was written for a specific library, so use it. And stop posting unformatted code that we find hard to read - there's a code tag button that retains the formatting.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
And stop posting unformatted code that we find hard to read - there's a code tag button that retains the formatting.

Instructions here: http://www.avrfreaks.net/wiki/ad...