8051 microcontroller 8051 microcontroller
Forums

Rickey's World of Microcontrollers & Microprocessors :: Forums :: Discuss and Learn :: 8051 Discussion Forum
 
<< Previous thread | Next thread >>
My learning thread - thanks everyone
Page 31 of 58    1 2 3 ... 30 [31] 32 ... 56 57 58
Moderators: Ajay Bhargav, Arun Kumar V, pdi33, Shailesh NAYAK, ۞ TPS ۞, shyam, sashijoseph, ExperimenterUK, DavesGarage, majoka
Author Post
romel_emperado
Mon Jan 10 2011, 06:11PM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
nice one guys! Haha

python is weird language but good...
Back to top
Ajay Bhargav
Tue Jan 11 2011, 12:55PM
Rickey's World Admin


Registered Member #1
Joined: Fri Feb 24 2006, 07:56AM
Location (Home Town): Punjab, India
Posts: 12590
do let me know when you make some progress
Back to top
romel_emperado
Tue Jan 11 2011, 06:59PM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
sure ajay. But its very difficult to make a progress hehe.
i have a based code but it is in tcl language my big problem is converting it to python. Wtf! Hehe

i cant get a help here since you don know tcl and python.
Back to top
romel_emperado
Tue Jan 11 2011, 07:01PM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
later i will attach the complete source code i will let you see what is tcl language..
Back to top
romel_emperado
Wed Jan 12 2011, 05:05AM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
guys what do you mean by this

CODE:
struct br
{
        // BLUEROBIN_OFF, BLUEROBIN_SEARCHING, BLUEROBIN_CONNECTED, BLUEROBIN_ERROR
        BlueRobin_state_t       state;
       
        // BLUEROBIN_NO_UPDATE, BLUEROBIN_NEW_DATA
        BlueRobin_update_t      update;
       
        // Chest strap ID      
        u32     cs_id;

        // User settings
        u8              user_sex;
        u16             user_weight;
       
        // Heart rate (1 bpm)
        u8              heartrate;
       
        // Calories (1 kCal) - calculated from heart rate, user weight and user sex
        u32     calories;
       
        // Speed (0.1 km/h) - demo version range is 0.0 to 25.5km/h
        u8              speed;
       
        // Distance (1 m)
        u32     distance;
       
        // 0=display calories, 1=display distance
        u8              caldist_view;
};
extern struct br sBlueRobin;


what do you mean by this extern struct br sBlueRobin;

and i found a function looks like this

CODE:
if (sBlueRobin.caldist_view == 0)       sBlueRobin.caldist_view = 1;
        else                                                            sBlueRobin.caldist_view = 0;



it seems that it calls the variable sBlueRobin.caldist_view

but why using "." or dot

when i will use struct br ??? what is br? is it variable?

when i declar a variable like this
CODE:
s32 weight;


what do the s32 mean?? hehe i need explaination.. Im using that in my code without understanding i just seen that in some examples in the web.. hehe

[ Edited Wed Jan 12 2011, 07:26AM ]
Back to top
romel_emperado
Wed Jan 12 2011, 07:08AM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
actually Im using that code above. I modified it and its working fine... i just used that without understanding and now i need someone to explain that to me.... hehehe eheheh thanks in advance..... heheh

Back to top
Ajay Bhargav
Wed Jan 12 2011, 09:48AM
Rickey's World Admin


Registered Member #1
Joined: Fri Feb 24 2006, 07:56AM
Location (Home Town): Punjab, India
Posts: 12590
thats how structures in C/C++ are used. it looks like you never used structure.
here is a quick reference

Defining structure
CODE:
//defining structure
struct mystruct {
    int a;
    char b;
    char c[10];
    int d[10];
};
 

Now mystruct is name of structure. a,b,c and d are members of structure. its like grouping set of variables in a single datatype and giving your own name to it.

to use a structure, you first need to create an instance of the structure. and members of structure can only be accessed through that instance.

CODE:
//defining instance of struct
struct mystruct x,y,*z;
 

so x and y are two instances of structure. and z is a pointer to mystruct.

using structure is very simple, lets say you want to equate members of structure x with y then you will do like this.
CODE:
x.a = y.a;
x.b = y.b;

memcpy(x.c, y.c, 10); //copy character array
 


Pointers work a little different way we use dot (.) incase of structure variables to access the members but incase of pointers we use arrows (->) e.g.
CODE:
z = &x; //z has address of x, or say z points to x now.
y.a = z->a; //you see the difference now?
y.b = z->b;
//its same with all.. just replace . with ->


Initializing structure:
structures are initialized just like we do in arrays. each member is separated by comma and complete thing is enclosed in curly brackets.
CODE:
x = {10, 'c', "hello", {10,20,30,40,50,60,70,80,90,100}};

//partially defining structure
y = {30, 'y', "world"};
//now the integer array  will automatically be initialized to 0.
 


Working example:
lets take an example, I want to make database of user accounts in C. I can have total of 100 users. user info will have username, password, age etc. so if you just do it in traditional style what you will do is.. you will create three arrays to store information of each user.
CODE:
//array style..
char username[100][20]; //100 usernames each of length 20 max
char password[100][15]; //100 passwords each of length 15 max
int age[100]; //array to store age of 100 users
 

now if you write code using the above style you will have to manage lot of arrays which might get your code look very complex. So now lets see structure style for the same..
CODE:
struct users {
char username[20];
char password[15];
int age;
};
 

that defines a custom data type with name users. now lets create 100 users.

CODE:
struct users user[100];
//we define array of 100 1 structure for each user.
 


lets make simple login checker now.. a function that checks if entered user details are correct or not.

CODE:
int checkuser(struct users input){
    int i;
    int status = 0;
    //we have 100 users so loop will go from 0 to 99
    //checking each user..
    for(i=0; i<100; i++){
        if(strcmp(input.username, user[i].username) == 0){ //check if username is there in database or not
            if(strcmp(input.password, user[i].password) == 0){ //check if password matches
                status = 1; //set status to 1 so that we say input user is available in database
                break; //break out of loop, we are done with checking
            } else {
                status = -1; //username is there but password incorrect
                break; //break out of loop.
            }
        }
    }
    if(i == 100){
        //if i reaches 100 it means username not available in database
        status = -2;
    }
    return status; //return status to caller function
}
 


well i think small tutorial on structures is good enough hope you now learn how to use structures, how to pass structure as a varaible to function, defining a structure pointer and variable etc.
 romel_emperado like this.
Back to top
romel_emperado
Thu Jan 13 2011, 12:37AM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
thank you very much ajay.. your tut is very detailed and understandable... thanks for your effort yar!!! heheeh I love u... hehe
Back to top
romel_emperado
Thu Jan 13 2011, 08:43AM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
how about this ajay

CODE:
#define DISPLAY_DEFAULT_VIEW                    (0u)


i know how to use #define varName but what is this
CODE:
(0u)
thing??
Back to top
romel_emperado
Fri Jan 14 2011, 10:17AM

Registered Member #31156
Joined: Sun Oct 03 2010, 01:40AM
Location (Home Town): Tibanga ILigan city Philippines
Posts: 1072
guys i need help again... How do i call the function mx_cal(); ?? what is that u8 line parameter? i dont have idea how do i call that from the function i made below which is burned_cal();



CODE:
// system
#include "project.h"

// driver
#include "display.h"
#include "ports.h"

// logic
#include "romel.h"
#include "clock.h"
#include "date.h"
#include "user.h"

// Global Variable section
struct br mel;
s8 weight2, time;
s8 met2;
float kcal;


// Prototypes section



void display_romel(u8 line, u8 update)
{
        if (update == DISPLAY_LINE_UPDATE_FULL)
        {
                display_chars(LCD_SEG_L2_5_0, (u8 *)" CAL0", SEG_ON);
        }
}



void mx_cal(u8 line)
{
        u8 select;
        s32 weight;
        s32 met;

        // Clear display
        clear_display_all();
       
        // Convert global variables to local variables
        //kcalories     =      mel.calories/1000;
        met                 =      mel.met_value/1000;
        if (sys.flag.use_metric_units)  weight = mel.user_weight;
        else                                                    weight = ((s32)mel.user_weight * 2205) / 1000; // Convert kg to lb

        // Init value index
        select = 0;
       
       
                // Loop values until all are set or user breaks set
        while(1)
        {
                // Idle timeout : exit without saving
                if (sys.flag.idle_timeout) break;

                // Button STAR (short): save, then exit
                if (button.flag.star)
                {
                        // Store local variables in global structure
                        //mel.calories  = kcalories*1000;
                        //mel.user_sex  = sex;
                        mel.met_value           = met*1000;
                        if (sys.flag.use_metric_units)  mel.user_weight = weight;
                        else                                                    mel.user_weight = (weight * 1000) / 2205;
                       
                        // Set display update flag
                        display.flag.line1_full_update = 1;

                        break;
                }

                switch (select)
                {

                        case 0: // Set user weight
                                    clear_line(LINE2);
                                                if (sys.flag.use_metric_units)
                                                {
                                                        display_chars(LCD_SEG_L2_1_0, (u8 *)"KG", SEG_ON);
                                                        set_value(&weight, 3, 2, USER_WEIGHT_MIN_KG, USER_WEIGHT_MAX_KG, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1);
                                                         
                                                }
                                                else
                                                {
                                                        display_chars(LCD_SEG_L2_1_0, (u8 *)"LB", SEG_ON);
                                                        set_value(&weight, 3, 2, USER_WEIGHT_MIN_LB, USER_WEIGHT_MAX_LB, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1);
                                                        weight2 = weight;
                                                }
                                                select = 1;
                                                break;
                                               
                        case 1:   //Set MET
                                                display_chars(LCD_SEG_L2_1_0, (u8 *)"ME", SEG_ON);
                                                set_value(&met, 3, 2, USER_WEIGHT_MIN, USER_METS_MAX, SETVALUE_DISPLAY_VALUE + SETVALUE_NEXT_VALUE, LCD_SEG_L2_4_2, display_value1);
                                                met2 = met;
                                                select = 0;
                                                break;
                                       

                }
        }      

        // Clear button flags
        button.all_flags = 0;
}


//calorie expernditure forMULa

void burned_cal(void)
{
                if(sTime.second == 60)
                {
                                time++;
                }
               
                kcal = ((met2 * 3.5 * (weight2/2.2)) / 200) * time;
}





 


[ Edited Sat Jan 15 2011, 03:09AM ]
Back to top
Page 31 of 58    1 2 3 ... 30 [31] 32 ... 56 57 58  

Jump:     Back to top

Syndicate this thread: rss 0.92 Syndicate this thread: rss 2.0 Syndicate this thread: RDF
Powered by e107 Forum System

© 2010 Rickey's World
Render time: 0.2161 sec, 0.0113 of that for queries. DB queries: 66. Memory Usage: 3,711kB