Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[MOVED] reading text file in KEIL C CODE

Status
Not open for further replies.

zizi110

Member level 1
Joined
Jun 24, 2013
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
313
hi all,
i want to read a text file in keil.
i found this code on net but it dosnt work in keil!!!
i test it in GCC compiler and it works! but i dont know why it dose not work in keil...
thanks


code:



Code dot - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <stdlib.h>
 
int txt2array(char *file_name)
{
 
    FILE *myFile;
    myFile = fopen(file_name, "r");
 
    //read file into array
    int N=499998;
    int numberArray[N];
    int i;
 
    if (myFile == NULL)
    {
        printf("Error Reading File\n");
        exit (0);
    }
    for (i = 0; i < N; i++)
    {
        fscanf(myFile, "%1d,", &numberArray[i] );
 
    }
 
    for (i = 0; i < N; i++)
    {
           printf("Number is: %1d\n\n", numberArray[i]);
        }
 
    fclose(myFile);
 
    return numberArray;
}

 
Last edited by a moderator:

Re: reading text file in KEIL C CODE

Hi,
What do you mean by "it doesn't work". Does it compile? If so, what is the output?
 

Re: reading text file in KEIL C CODE

Hi ARQuattr,
Yes it complile( with no error) , but i dont have the output in simulator of keil.
i said if the digit in the text file that is reading is one then ==== > PORTA.1 of micro is ON
and if the digit in the text file is zero then ===> PORTA.1 of micro is OFF
the text file contans seris of 0 and 1.
my microcontroller :AT91SAM7X256
my compiler: KEIL

C code in KEIL:



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <AT91SAM7X256.h>
#include <stdio.h>
#include <stdlib.h>
#include "delay.h"
 
int main()
{
 
      int N=40;;
    int numberArray[40];
    int i;
      FILE *myFile;
    *AT91C_PIOA_PER = (AT91C_PIO_PA1)|(AT91C_PIO_PA2);  //LED 1
      *AT91C_PIOA_OER = (AT91C_PIO_PA1)|(AT91C_PIO_PA2);  //LED 2    
    
     myFile = fopen("kk.txt", "r");
 
    //read file into array
 
 
    if (myFile == NULL)
    {
        printf("Error Reading File\n");
        exit (0);
    }
    for (i = 0; i < N; i++)
    {
        fscanf(myFile, "%1d,", &numberArray[i] );
 
    }
        
        for (i = 0; i < N; i++){
            if(numberArray[i] == 1){
                *AT91C_PIOA_SODR=(AT91C_PIO_PA1);   // if the digit is one : PA1 is ON 
                delay_ms(300);
            }
            else{
                *AT91C_PIOA_CODR=(AT91C_PIO_PA1);    // if the digit is zero: PA1 is OFF
                delay_ms(300);
            }
        }
 
   fclose(myFile);
 
    return 0;
}



thanks for your time
 
Last edited by a moderator:

Re: reading text file in KEIL C CODE

For successful file I/O, a storage device must be connected to the processor and accessed by the low-level I/O routines. What did you connect?

There may be specific simulator features to mimic a storage device. If so you'll find a description in the simulator user manual. Did you review the manual regarding simulated file I/O?
 

Re: reading text file in KEIL C CODE

Hi fVM,
thanks for your reply.
i dont have any storage module!
is it the only way?(using storage module)
in fact the text file is the output of simulink model, is there any way that i connect simulink to microcontroller? (AT91SAM)
or is there any way that i could implement simulink model on my microcontroller?

----------------
regards
 

Re: reading text file in KEIL C CODE

Then actually you don need a text file reader in your controller instead write the code in windows to send the required bytes of text file through RS232 and then write a program in controller to work with the received data.

or if you know how to access a parallel port in your windows system then may be you will not need a controller.
 

Re: reading text file in KEIL C CODE

Hi Venkadesh_M
thanks for your point, but i dont wanna use serial connection.
 

Re: reading text file in KEIL C CODE

What is the reason for avoiding UART ? what about parallel port or USB to serial or direct usb ?

What are you doing with simulink ? Is that text will be keep updated ? in what interval ? You have to use a system only for this application. If you don want to use a system, I think you have to go for some linux based platforms if simulink is avail for linux.

If the text file will not be changed then you can just port the data in controller or you can store it on EEPROM or a SD card.
 
i used usb to serial madule, and it works very well ... but my supervisor doesnt agree with serial connection.
no that text is fixed and no updating.
i design a sigma delta modulator in simulink and actually text file is the output of this model. it contains a series of 0 and 1( on 10 sec interval simulation in simulink)
and i must give this text file to micro, so yes i think i must use SD card or any storage device!
maybe i can define that text file into a array in C code ... maybe it is better.
any way thanks a lot for your points.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top