Gå til innhold

Komme i gang med C


Anbefalte innlegg

Kan ikke noe om programmering eller hvilket språk som kan gjøre hva.

 

Det jeg ønsker å lære er:

 

Programmere en PIC microcontroller. Dette gjøres ved hjelp av C i produsentens programvare.

 

Kunne lage et program på en pc som ved et grafisk grensesnitt lar deg vise informasjon fra microcontrolleren.

 

Håper C er godt egnet til begge deler.

Noen som kan gi et godt tips om en god lærebok for dette?

 

Er det sider ala codecademy.com for å lære seg C?

 

 

EDIT:

 

Da denne tråden har gitt meg svært mye hjelp med å skrive mitt første program, velger jeg å oppsummere fremgangen i denne posten.

 

For hver nye forbedring av mitt program, har jeg laget en ny versjon, og jeg legger inn kopi av disse versonene i spoilerene under. Der er da lettere å se fremgangen som er gjort i programmet dersom andre er i samme situasjon som meg. Dersom man skulle ha spørsmål om det som er gjort fra versjon til versjon er nok det meste blitt besvart gjennom denne tråden.

 

Temperature conversion v 1.0

 

 

#include <stdio.h>

int main()
{
    /*
    Terje Andersen
    26/10-2017

    Temperature conversion v 1.0
    */

    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer;

    printf("Please enter temperature in celsius: ");
    scanf("%f", &celsius );

    fahrenheit = (celsius * 1.80) + 32.;
    kelvin = celsius + 273.15;
    rankine = (celsius * 1.80) + 491.67;
    delisle = (celsius * (3. / 2.)) - 100.;
    newton = celsius * (33. / 100.);
    reaumur = celsius * (4. / 5.);
    romer = (celsius * (21. / 40.)) + 7.50;

    printf( "\nThe temperature in fahrenheit is \t %6.2f \n", fahrenheit);
    printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
    printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
    printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
    printf( "The temperature in newton is \t\t %6.2f \n", newton);
    printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
    printf( "The temperature in romer is \t\t %6.2f \n", romer);
    return 0;
} 

 

 

 

Temperature conversion v 1.1

 

 

#include <stdio.h>

/*
Terje Andersen
27/10-2017

Temperature conversion v 1.1
*/


int main()
{
    printf("Temperature conversion tool\n\n");
    printf("Choose what type to convert from:\n");
    printf("[1] Celsius\n");
    printf("[2] Delisle\n");
    printf("[3] Fahrenheit\n");
    printf("[4] Kelvin\n");
    printf("[5] Newton\n");
    printf("[6] Rankie\n");
    printf("[7] Reaumur\n");
    printf("[8] R\x9bmer\n\n");
    printf("Please enter temperature type: ");

    int temperature_type;
    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer, calculation;

    scanf("%d", &temperature_type);
    if (temperature_type == 1) {
        printf("Please enter temperature in celsius: ");
        scanf("%f", &celsius );
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 2) {
        printf("Please enter temperature in delisle: ");
        scanf("%f", &calculation);
        celsius = (100.00 - calculation * (2.00 / 3.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 3) {
        printf("Please enter temperature in fahrenheit: ");
        scanf("%f", &calculation);
        celsius = ((calculation - 32.00) * (5.00 / 9.00));
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 4) {
        printf("Please enter temperature in kelvin: ");
        scanf("%f", &calculation);
        celsius = (calculation - 273.15);
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 5) {
        printf("Please enter temperature in newton: ");
        scanf("%f", &calculation);
        celsius = (calculation * (100.00 / 33.00));
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 6) {
        printf("Please enter temperature in rankine: ");
        scanf("%f", &calculation);
        celsius = ((calculation - 491.67) * (5.00 / 9.00));
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        reaumur = (celsius * (4.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 7) {
        printf("Please enter temperature in reaumur: ");
        scanf("%f", &calculation);
        celsius = (calculation * (5.00 / 4.00));
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        romer = ((celsius * (21.00 / 40.00)) + 7.50);
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 8) {
        printf("Please enter temperature in r\x9bmer: ");
        scanf("%f", &calculation);
        celsius = ((calculation - 7.50) * (40.00 / 21.00));
        delisle = ((100.00 - celsius) * (3.00 / 2.00));
        fahrenheit = ((celsius * 1.80) + 32.00);
        kelvin = (celsius + 273.15);
        newton = (celsius * (33.00 / 100.00));
        rankine = ((celsius + 273.15) * (9.00 / 5.00));
        reaumur = (celsius * (4.00 / 5.00));
        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
    }
    else {
        printf("Please choose a number, 1-8, from above!\n\n");
    }

    return 0;
}
/*
v 1.0
    Conversion from celsius

v 1.1
    Added menu / option to convert both ways
*/ 

 

 

 

Temperature conversion v 1.2

 

 

#include <stdio.h>

/*
Terje Andersen
27/10-2017

Temperature conversion v 1.2
*/

int main()
{
    while(1){

    printf("Temperature conversion tool\n\n");
    printf("Choose what type to convert from:\n");
    printf("[1] Celsius\n");
    printf("[2] Delisle\n");
    printf("[3] Fahrenheit\n");
    printf("[4] Kelvin\n");
    printf("[5] Newton\n");
    printf("[6] Rankie\n");
    printf("[7] Reaumur\n");
    printf("[8] Romer\n");
    printf("[0] Exit program\n\n");
    printf("Please enter temperature type: ");

    int temperature_type;
    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer, convert_to_celsius;

    void convert_temperatures(
        float celsius) {
        delisle = (celsius * (3. / 2.)) - 100;
        fahrenheit = (celsius * 1.80) + 32.;
        kelvin = celsius + 273.15;
        newton = celsius * (33. / 100.);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4. / 5.);
        romer = (celsius * (21. / 40.)) + 7.50;
    }

    scanf("%d", &temperature_type);

    if (temperature_type == 1) {
        printf("Please enter temperature in celsius: ");
        scanf("%f", &celsius );
        convert_temperatures(celsius);

        printf( "\nThe temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 2) {
        printf("Please enter temperature in delisle: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius + 100) * (2. / 3.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 3) {
        printf("Please enter temperature in fahrenheit: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 32.) * (5. / 9.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 4) {
        printf("Please enter temperature in kelvin: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius - 273.15;
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 5) {
        printf("Please enter temperature in newton: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (100. / 33.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 6) {
        printf("Please enter temperature in rankine: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 491.67) * (5. / 9.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 7) {
        printf("Please enter temperature in reaumur: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (5. / 4.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in romer is \t\t %6.2f \n", romer);
    }
    else if (temperature_type == 8) {
        printf("Please enter temperature in romer: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 7.5)* (40. / 21.);
        convert_temperatures(celsius);

        printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
        printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        printf( "The temperature in newton is \t\t %6.2f \n", newton);
        printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
    }
    else if (temperature_type == 0) {
        exit(0);
    }
    else {
        printf("Please choose a number, 1-8, from above!\n\n");
    }

    system ("pause");
    system("cls");

    } // while

    return 0;
} // main

/*
v 1.0
    Conversion from celsius

v 1.1
    Added menu / option to convert both ways

v 1.2
    Moved calculations out of IF statement
    Verified formulas against www.metric-conversions.org
        celsius = (fahrenheit - 32.) * (5. / 9.)
        celsius = kelvin - 273.15
        celsius = (rankine - 491.67) * (5. / 9.)
        celsius = (delisle + 100) * (2. / 3.)
        celsius = newton * (100. / 33.)
        celsius = reaumur * (5. / 4.)
        celsius = (romer - 7.5)* (40. / 21.)

        delisle = (celsius * (3. / 2.)) - 100;
        fahrenheit = (celsius * 1.80) + 32.;
        kelvin = celsius + 273.15;
        newton = celsius * (33. / 100.);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4. / 5.);
        romer = (celsius * (21. / 40.)) + 7.50;
    Removed errors from calculations
    Added:
        - While function to loop the program
        - Pause function
        - Clear screen, between menu choices
        - Program exit as menu option
*/ 

 

 

 

Temperature conversion v 1.3

 

 

#include <stdio.h>

/*
Terje Andersen
27/10-2017

Temperature conversion v 1.3
Program to learn to code in C
Full progress info at: https://www.diskusjon.no/index.php?showtopic=1792480
*/

int main()
{
    while(1){ // Loop the program until exit is chosen

    
    int temperature_type;
    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer, convert_to_celsius;

    // Calculations to convert from celsius. Used in menu IF function
    void convert_temperatures(
        float celsius) {
        delisle = (celsius * (3.0 / 2.0)) - 100.0;
        fahrenheit = (celsius * 1.80) + 32.0;
        kelvin = celsius + 273.15;
        newton = celsius * (33.0 / 100.0);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4.0 / 5.0);
        romer = (celsius * (21.0 / 40.0)) + 7.50;
    }

    // Print options, used in menu IF function
    void print_temp() {
        printf("\n");
        if (temperature_type == 1 ) {
            printf( "\nThe temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 2 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 3 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 4 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 5 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 6 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 7 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in romer is \t\t %6.2f \n", romer);
        }
        if (temperature_type == 8 ) {
            printf( "\nThe temperature in celsius is \t\t %6.2f \n", celsius);
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        }
    } // void print_temp

    // Main menu
    printf("Temperature conversion tool\n\n");
    printf("Choose what type to convert from:\n");
    printf("[1] Celsius\n");
    printf("[2] Delisle\n");
    printf("[3] Fahrenheit\n");
    printf("[4] Kelvin\n");
    printf("[5] Newton\n");
    printf("[6] Rankie\n");
    printf("[7] Reaumur\n");
    printf("[8] Romer\n");
    printf("[0] Exit program\n\n");
    printf("Please enter temperature type: ");
    scanf("%d", &temperature_type);

    if (temperature_type == 1) {
        printf("Please enter temperature in celsius: ");
        scanf("%f", &celsius );
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 2) {
        printf("Please enter temperature in delisle: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius + 100.0) * (2.0 / 3.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 3) {
        printf("Please enter temperature in fahrenheit: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 32.0) * (5.0 / 9.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 4) {
        printf("Please enter temperature in kelvin: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius - 273.15;
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 5) {
        printf("Please enter temperature in newton: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (100.0 / 33.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 6) {
        printf("Please enter temperature in rankine: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 491.67) * (5.0 / 9.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 7) {
        printf("Please enter temperature in reaumur: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (5.0 / 4.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 8) {
        printf("Please enter temperature in romer: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 7.5)* (40.0 / 21.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 0) {
        exit(0);
    }
    else {
        printf("Please choose a number, 1-8, from above!\n\n");
    }

    system ("pause");
    system("cls");

    } // while

    return 0;
} // main

/*
v 1.3
    Code cleanup
    Added code explanations
    Moved printf into seperate void function

v 1.2
    Moved calculations out of IF statement
    Verified formulas against www.metric-conversions.org
        celsius = (fahrenheit - 32.) * (5. / 9.)
        celsius = kelvin - 273.15
        celsius = (rankine - 491.67) * (5. / 9.)
        celsius = (delisle + 100) * (2. / 3.)
        celsius = newton * (100. / 33.)
        celsius = reaumur * (5. / 4.)
        celsius = (romer - 7.5)* (40. / 21.)

        delisle = (celsius * (3. / 2.)) - 100;
        fahrenheit = (celsius * 1.80) + 32.;
        kelvin = celsius + 273.15;
        newton = celsius * (33. / 100.);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4. / 5.);
        romer = (celsius * (21. / 40.)) + 7.50;
    Removed errors from calculations
    Added:
        - While function to loop the program
        - Pause function
        - Clear screen, between menu choices
        - Program exit as menu option

v 1.1
    Added menu / option to convert both ways

v 1.0
    Conversion from celsius
*/ 

 

 

 

Temperature conversion v 1.4

 

 

#include <stdio.h>

/*
Terje Andersen
27/10-2017

Temperature conversion v 1.4
Program to learn to code in C
Full progress info at: https://www.diskusjon.no/index.php?showtopic=1792480
*/

int main()
{
    while(1){ // Loop the program until exit is chosen


    int temperature_type;
    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer, convert_to_celsius;

    // Calculations to convert from celsius. Used in menu IF function
    void convert_temperatures(
        float celsius) {
        delisle = (celsius * (3.0 / 2.0)) - 100.0;
        fahrenheit = (celsius * 1.80) + 32.0;
        kelvin = celsius + 273.15;
        newton = celsius * (33.0 / 100.0);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4.0 / 5.0);
        romer = (celsius * (21.0 / 40.0)) + 7.50;
    }

    // Print options, used in menu IF function
    void print_temp() {
	printf( "\n" );
	if ( temperature_type != 1 )
	    printf( "The temperature in celsius is \t\t %6.2f \n", celsius);
	if ( temperature_type != 2 )
	    printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
	if ( temperature_type != 3 )
	    printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
	if ( temperature_type != 4 )
	    printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
	if ( temperature_type != 5 )
	    printf( "The temperature in newton is \t\t %6.2f \n", newton);
	if ( temperature_type != 6 )
	    printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
	if ( temperature_type != 7 )
	    printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
	if ( temperature_type != 8 )
	    printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    } // void print_temp

    // Main menu
    printf("Temperature conversion tool\n\n");
    printf("Choose what type to convert from:\n");
    printf("[1] Celsius\n");
    printf("[2] Delisle\n");
    printf("[3] Fahrenheit\n");
    printf("[4] Kelvin\n");
    printf("[5] Newton\n");
    printf("[6] Rankie\n");
    printf("[7] Reaumur\n");
    printf("[8] Romer\n");
    printf("[0] Exit program\n\n");
    printf("Please enter temperature type: ");
    scanf("%d", &temperature_type);

    if (temperature_type == 1) {
        printf("Please enter temperature in celsius: ");
        scanf("%f", &celsius );
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 2) {
        printf("Please enter temperature in delisle: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius + 100.0) * (2.0 / 3.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 3) {
        printf("Please enter temperature in fahrenheit: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 32.0) * (5.0 / 9.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 4) {
        printf("Please enter temperature in kelvin: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius - 273.15;
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 5) {
        printf("Please enter temperature in newton: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (100.0 / 33.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 6) {
        printf("Please enter temperature in rankine: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 491.67) * (5.0 / 9.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 7) {
        printf("Please enter temperature in reaumur: ");
        scanf("%f", &convert_to_celsius);
        celsius = convert_to_celsius * (5.0 / 4.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 8) {
        printf("Please enter temperature in romer: ");
        scanf("%f", &convert_to_celsius);
        celsius = (convert_to_celsius - 7.5)* (40.0 / 21.0);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 0) {
        break;
    }
    else {
        printf("Please choose a number, 1-8, from above!\n\n");
    }

    system ("pause");
    system("cls");

    } // while

    return 0;
} // main

/*
v 1.4
    Changed the exit of the program from "exit(0);" to "break"
    Compressed the void print_temp function

v 1.3
    Code cleanup
    Added code explanations
    Moved printf into seperate void function

v 1.2
    Moved calculations out of IF statement
    Verified formulas against www.metric-conversions.org
        celsius = (fahrenheit - 32.) * (5. / 9.)
        celsius = kelvin - 273.15
        celsius = (rankine - 491.67) * (5. / 9.)
        celsius = (delisle + 100) * (2. / 3.)
        celsius = newton * (100. / 33.)
        celsius = reaumur * (5. / 4.)
        celsius = (romer - 7.5)* (40. / 21.)

        delisle = (celsius * (3. / 2.)) - 100;
        fahrenheit = (celsius * 1.80) + 32.;
        kelvin = celsius + 273.15;
        newton = celsius * (33. / 100.);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4. / 5.);
        romer = (celsius * (21. / 40.)) + 7.50;
    Removed errors from calculations
    Added:
        - While function to loop the program
        - Pause function
        - Clear screen, between menu choices
        - Program exit as menu option

v 1.1
    Added menu / option to convert both ways

v 1.0
    Conversion from celsius
*/
 

 

 

 

Temperature conversion v 1.5

 

 

#include <stdio.h>

/*
Terje Andersen
27/10-2017

Temperature conversion v 1.5
Program to learn to code in C
Full progress info at: https://www.diskusjon.no/index.php?showtopic=1792480
*/

int main()
{
    while(1){ // Loop the program until exit is chosen


    int temperature_type;
    float celsius, fahrenheit, kelvin, rankine, delisle, newton, reaumur, romer, celsius_calc;

    // Calculations to convert from celsius. Used in menu IF function
    void convert_temperatures(
        float celsius) {
        delisle = (celsius * (3.0 / 2.0)) - 100.0;
        fahrenheit = (celsius * 1.80) + 32.0;
        kelvin = celsius + 273.15;
        newton = celsius * (33.0 / 100.0);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4.0 / 5.0);
        romer = (celsius * (21.0 / 40.0)) + 7.50;
    }

    // Calculations to convert to celsius. Used in menu IF function
    void convert_to_celsius(
        float celsius_calc) {
        if ( temperature_type == 2 )
            celsius = (celsius_calc + 100.0) * (2.0 / 3.0);
        if ( temperature_type == 3 )
            celsius = (celsius_calc - 32.0) * (5.0 / 9.0);
        if ( temperature_type == 4 )
            celsius = celsius_calc - 273.15;
        if ( temperature_type == 5 )
            celsius = celsius_calc * (100.0 / 33.0);
        if ( temperature_type == 6 )
            celsius = (celsius_calc - 491.67) * (5.0 / 9.0);
        if ( temperature_type == 7 )
            celsius = celsius_calc * (5.0 / 4.0);
        if ( temperature_type == 8 )
            celsius = (celsius_calc - 7.5)* (40.0 / 21.0);
    } // void print_temp

    // Print options, used in menu IF function
    void print_temp() {
        printf( "\n" );
        if ( temperature_type != 1 )
            printf( "The temperature in celsius is \t\t %6.2f \n", celsius);
        if ( temperature_type != 2 )
            printf( "The temperature in delisle is \t\t %6.2f \n", delisle);
        if ( temperature_type != 3 )
            printf( "The temperature in fahrenheit is \t %6.2f \n", fahrenheit);
        if ( temperature_type != 4 )
            printf( "The temperature in kelvin is \t\t %6.2f \n", kelvin);
        if ( temperature_type != 5 )
            printf( "The temperature in newton is \t\t %6.2f \n", newton);
        if ( temperature_type != 6 )
            printf( "The temperature in rankine is \t\t %6.2f \n", rankine);
        if ( temperature_type != 7 )
            printf( "The temperature in reaumur is \t\t %6.2f \n", reaumur);
        if ( temperature_type != 8 )
            printf( "The temperature in r\x9bmer is \t\t %6.2f \n", romer);
    } // void print_temp

    // Main menu
    printf("Temperature conversion tool\n\n");
    printf("Choose what type to convert from:\n");
    printf("[1] Celsius\n");
    printf("[2] Delisle\n");
    printf("[3] Fahrenheit\n");
    printf("[4] Kelvin\n");
    printf("[5] Newton\n");
    printf("[6] Rankine\n");
    printf("[7] Reaumur\n");
    printf("[8] Romer\n");
    printf("[0] Exit program\n\n");
    printf("Please enter temperature type: " );
    scanf("%d", &temperature_type);

    if (temperature_type == 1) {
        printf("Please enter temperature in celsius: ");
        scanf("%f", &celsius );
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 2) {
        printf("Please enter temperature in delisle: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 3) {
        printf("Please enter temperature in fahrenheit: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 4) {
        printf("Please enter temperature in kelvin: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 5) {
        printf("Please enter temperature in newton: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 6) {
        printf("Please enter temperature in rankine: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 7) {
        printf("Please enter temperature in reaumur: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 8) {
        printf("Please enter temperature in romer: ");
        scanf("%f", &celsius_calc);
        convert_to_celsius(celsius_calc);
        convert_temperatures(celsius);
        print_temp();
    }
    else if (temperature_type == 0) {
        break;
    }
    else {
        printf("Please choose a number, 1-8, from above!\n\n");
    }

    printf("\nPress a ENTER to continue...");
    getchar(); // remove the \n that is left in memory from scanf()
    getchar(); // hold the program waiting for enter to be pressed
    system("cls");

    } // while

    return 0;
} // main
/*
v 1.5
    Moved the rest of the calculations out of IF into seperate function
    Replaced "system ("pause");" with getchar()

v 1.4
    Changed the exit of the program from "exit(0);" to "break"
    Compressed the void print_temp function

v 1.3
    Code cleanup
    Added code explanations
    Moved printf into seperate void function

v 1.2
    Moved calculations out of IF statement
    Verified formulas against www.metric-conversions.org
        celsius = (fahrenheit - 32.) * (5. / 9.)
        celsius = kelvin - 273.15
        celsius = (rankine - 491.67) * (5. / 9.)
        celsius = (delisle + 100) * (2. / 3.)
        celsius = newton * (100. / 33.)
        celsius = reaumur * (5. / 4.)
        celsius = (romer - 7.5)* (40. / 21.)

        delisle = (celsius * (3. / 2.)) - 100;
        fahrenheit = (celsius * 1.80) + 32.;
        kelvin = celsius + 273.15;
        newton = celsius * (33. / 100.);
        rankine = (celsius * 1.80) + 491.67;
        reaumur = celsius * (4. / 5.);
        romer = (celsius * (21. / 40.)) + 7.50;
    Removed errors from calculations
    Added:
        - While function to loop the program
        - Pause function
        - Clear screen, between menu choices
        - Program exit as menu option

v 1.1
    Added menu / option to convert both ways

v 1.0
    Conversion from celsius
*/
 

 

 

 

Versjons logg

 

 

 

v 1.5
    Moved the rest of the calculations out of IF into seperate function
    Replaced "system ("pause");" with getchar()
 

v 1.4
Changed the exit of the program from "exit(0);" to "break"
Compressed the void print_temp function

v 1.3
Code cleanup
Added code explanations
Moved printf into seperate void function

v 1.2
Moved calculations out of IF statement
Verified formulas against www.metric-conversions.org
celsius = (fahrenheit - 32.) * (5. / 9.)
celsius = kelvin - 273.15
celsius = (rankine - 491.67) * (5. / 9.)
celsius = (delisle + 100) * (2. / 3.)
celsius = newton * (100. / 33.)
celsius = reaumur * (5. / 4.)
celsius = (romer - 7.5)* (40. / 21.)

delisle = (celsius * (3. / 2.)) - 100;
fahrenheit = (celsius * 1.80) + 32.;
kelvin = celsius + 273.15;
newton = celsius * (33. / 100.);
rankine = (celsius * 1.80) + 491.67;
reaumur = celsius * (4. / 5.);
romer = (celsius * (21. / 40.)) + 7.50;
Removed errors from calculations
Added:
- While function to loop the program
- Pause function
- Clear screen, between menu choices
- Program exit as menu option

v 1.1
Added menu / option to convert both ways

v 1.0
Conversion from celsius
*/

 

 

Endret av Teza
Lenke til kommentar
Videoannonse
Annonse

Å lære seg C er slett ikke dumt, da veldig mange moderne språk bygger på C.

 

Det er en del konsepter og begreper som kan være vanskelig å forstå i C, og som er nærmest fraværende i mer moderne språk. Pekere og minnehåndtering, for å nevne et par.

 

Ellers er vel dette et greit utgangspunkt, hva tutorials angår:

 

https://www.tutorialspoint.com/cprogramming/c_useful_resources.htm

  • Liker 2
Lenke til kommentar

Hvilken PIC er det du vil programmere?

 

Start med å laste ned http://www.microchip.com/mplab/mplab-x-ide så kan du se på noen eksempler for chipen du har valgt deg ut.

 

For kommunikasjon med en PC ville jeg valgt Serial eller USB, kommer litt an på hva du skal kommunisere.

Normalt sett ville jeg ikke anbefalt C for GUI, særlig ikke når du ikke kan C fra før. Men ingenting er umulig, du kan hvis du vil.

Dersom du vil holde deg til C for gui anbefaler jeg https://www.gtk.org/. Hvis du vil lære deg C++ er https://www.qt.io/ vanskelig å slå. Kommer også men en fin editor og masse eksempelkode. For eksempel noe herifra http://doc.qt.io/qt-5/qtserialport-examples.html

 

Når du forteller meg hvilken PIC du har valgt kan jeg hjelpe med å finne eksempel for Serial som fungerer med Qt eksempelene.

 

 

Lykke til!

  • Liker 1
Lenke til kommentar

Har lastet ned MPLAB X IDE v3.65 og XC8 compiler, og brukt dette for å få en PIC16F88_I/P til å blinke en lysdiode.

Dog har jeg problemer med å forstå C koden og ønsker derfor å lære denne.

 

Håpet er at jeg kan lære ett programeringsspråk, for PIC og for å klare å kommuniserer med denne microcontrolleren via en PC.

Prøver å finne en enkel praktisk måte å lære dette på.

 

Bruksområdet jeg ønsker å utforske er å lese av flere typer sensorer, lagre avlesningene og kunne vise informasjonen på skjermen.

Lenke til kommentar

Post koden din, så kan jeg forklare det du lurer på.

Hands on er bedre enn å lese, spør du meg (jobbet som veileder i studietiden). En annen ting er at mye kunnskap du ser fra desktop C brukes sjelden i kodeeksempler i embedded.

 

Antakelig er mye av det du føler du ikke forstår pga hardware ikke på grunn av C.

Boken som er linket er dog veldig god, og anbefaler absolutt at du leser den! Men du lærer programmering av å programmere :)

Lenke til kommentar
Gjest Slettet+5132

Det begynner å bli et par år siden jeg lærte C, men jeg brukte Expert C Programming som bok for viderekommende (først var det K&R). Det finnes også nyere C-versjoner, men hvis du programmerer mot embedded er det ikke sikkert disse er støttet. 

 

Tror kaaaanskje jeg hadde spandert en dag eller to til å lære høynivå assembler for å forstå hva som foregår på et litt lavere nivå.

 

Hvis du vil lære å strukture koden kan det være verdt å ta en titt på C Interfaces and Implementations: Techniques for Creating Reusable Software   (ADVARSEL: IKKE bruk exceptions slik det er implementert der, det gir deg med 99% sannsynlighet minnelekasje).  Denne boka vil også hjelpe å forstå ting som GTK hvis du senere vil gjøre litt grafiske brukergrensersnitt, dog her vil jeg nok kanskje anbefale å gå over til C++, eller til og med lage et grensesnitt i et annet språk.

Endret av Slettet+5132
Lenke til kommentar

Assembler er fornuftig for best mulig ytelse, men det øker også kompleksiteten. Kombinerer man in-line assembly i C-koden får man det beste fra begge verdener. Fordelene med et høynivåspråk som C er at koden blir mer håndterbar og at man heller bruker assembly der hastigheten og spesialiseringen er viktig når du har behov for dette. Eksempelvis i tidskritiske indre looper eller der timingen må være tight. I dag er utstyr sjelden begrenset så mye av arbeidsminne at det forvarer å kjøre hele prosjektet i assembly. 

Lenke til kommentar

Post koden din, så kan jeg forklare det du lurer på.

 

 

Prøver å dokumentere prosjektene mine så jeg kan gå tilbake for å se hva jeg har tenkt og hva jeg har gjort.

 

Du finner prosjektet mitt i følgende pdf fil: AAB.pdf

På side 11 finner du koden jeg har brukt.

 

Kort oppsummert:

 

PIC kjører på 4 MHz (clock speed), hvilket gjør at programmet skal kjøre på 1 MHz (instruction cycle speed) (forklart på side 26).

 

Basert på dette antar jeg at delay angir 500 mS, men forstår ikke koden lengre opp med å telle ned fra 100.

Endret av Teza
  • Liker 1
Lenke til kommentar

 

 

 

 

 

Henger ikke med, ta i betraktning at jeg først i dag har funnet ut hvilken programvare jeg kan bruke for å komme i gang, og har sett på "Hello world" programmet  :)

 

Jeg trenger å finne den enkleste måten å lære C på.

Forhåpentlig vis slipper jeg å lære to programmeringsspråk for å klare prosjektmålet mitt :)

Lenke til kommentar
Gjest Slettet+5132

 

 

 

 

 

 

Henger ikke med, ta i betraktning at jeg først i dag har funnet ut hvilken programvare jeg kan bruke for å komme i gang, og har sett på "Hello world" programmet  :)

 

Jeg trenger å finne den enkleste måten å lære C på.

Forhåpentlig vis slipper jeg å lære to programmeringsspråk for å klare prosjektmålet mitt :)

 

 

Hvis det er assemblerdelen du reagerer på: Det var mer som supplement til C, spesielt hvis du skla begynne med embedded. Det gir deg innblikk i "Det mest elementære språket" du kan skrive, og nøyaktig hvilke operasjoner du kan be en PC å utføre. Jeg sier ikke at du bør lære deg assembler, men kanskje når du har begynt å få en liten følelse for C, kan det være greit å se på enkle eksempler i assembler for å se hva C faktisk gjør automatisk for deg (på sett og vis er det ikke veldig mye C gjør automatisk for deg, men det er ting som registere det kan lønne seg å ha et forhold til på embedded).

 

EDIT: Ellers er jeg vel enig i at du bør bruke mye tid til eksperimentering. C er et språk man trenger å utvikle litt følelse for.

Endret av Slettet+5132
Lenke til kommentar

Helt vanvittig dokumentasjon! Bra jobbet! :D

 

Du har som du selv sier 1MHz instruction cycle, det betyr at hver gang du gjør en operasjon koster det 1/1000000 av et sekund. Siden delay input er oppgitt i milliseconds, så kan vi først tenke at vi deler 1MHz på 1000 (milliseconds). Da ser vi at for hver millisecond må vi bruke opp 1000 sykluser fordi 1000000 / 1000 = 1000. Hvorfor koden din bruker 100 og ikke 1000 er jeg usikker på, med mindre den går i den while loopen ti ganger?

 

Uansett, while loop fungerer slik du har skrevet i dokumentasjon.

 

Så lenge det jeg tester ikke er null, så fortsetter jeg å gjøre det jeg er bedt om.

while (iWantToWasteTime) {
  // please do these things when I want to waste time
  iWantToWasteTime--; // getting closer to zero every time the while loop does the things
}
// finally, iWantToWasteTime is actually zero, the while loop is done, no more time to waste!

En delay kaster bort tid på gjøre noe. Enten det er å plusse et nummer med et annet, eller en såkalt NoOperation. Nop.

 

Har du skrevet dokumentasjonen selv?

 

 

EDIT: Hvis du legger til #define _XTAL_FREQ 4000000 så kan du bruke __delay_ms(1000); for delays (her for ca ett sekund). Er med som standard fra xc.h mener jeg å huske.

Endret av Enthroner
Lenke til kommentar

Dokumentasjonen har jeg satt sammen selv basert på mange timer lesning fra mange kilder.

Koden har jeg funnet på nettet og tilpasset den til mitt prosjekt.

 

Delay bruker så langt jeg forstår det 0,1 mS på å kjøre 1 gang (0,001 mS x 100)

While kjører delay 500 ganger, som skulle gi 50 mS ikke 500 mS som den gjør i praksis.

 

#define _XTAL_FREQ 4000000

 

Denne forutsetter vel at det er en  4 MHz krystall i kretsen og at #pragma config FOSC = XT?

 

 

Edit:

 

Men denne tråden fokuserer mer på hvordan jeg kan lære C.

Å finne ut hva som er feil i prosjektet mitt burde være i en separat tråd :)

Endret av Teza
Lenke til kommentar

 

Dersom du vil holde deg til C for gui anbefaler jeg https://www.gtk.org/

 

 

Lastet ned GTK og oppdaterte denne.

 

ser ut som  #include <gtk/gtk.h> mangler, når jeg prøver eksemplelet (i Code::Blocks) fra siden:

https://developer.gnome.org/gtk3/stable/gtk-getting-started.html

 

Vil tro jeg ikke har fått installert alt?

Har søkt etter filen i installasjonsmappen, men den finnes ikke.

 

Garantert nybegynnerfeil fra min side  :)

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...