Connect with us

I2C programming

Discussion in 'Microcontrollers, Programming and IoT' started by Wantas, May 30, 2015.

Scroll to continue with content
  1. Wantas

    Wantas

    4
    0
    May 30, 2015
    Hi everyone Can you help me ?? This code below is for CAN interface and tranceiver.. I'm green in this thing.. so I want to ask you how to write a code for I2C interface and Digital to analog converter..

    Code:
    #include "inc/hw_memmap.h"
    #include "inc/hw_ty pes.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_can.h"
    #include "driverlib/can.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "utils/uartstdio.h"
    
    
    
    int main(void)
    {
    tCANMsgObject zinute;
    int i = 0;
    unsigned char duomenys[30];
    
    
    zinute.ulMsgID = 1;  // CAN zinutes ID
    zinute.ulMsgIDMask = 0;  // kauke naudojama kai yra ID filtravimas
    zinute.ulFlags = 0;     // veliavele
    
    
    //nustatomas taktavimas is isorinio taktiniu impulsu generatoriaus
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
      SYSCTL_XTAL_16MHZ);
    
    
    //ijungiamas bendros paskiries D portas kuriame yra reikalingi pinai
    
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    
    
    //nustatoma alternatyvi funkcija GPIO pinams.
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
     
    
    // pinams priskiriamos CAN0 funkcijos 
    GPIOPinConfigure(GPIO_PD0_CAN0RX);
    GPIOPinConfigure(GPIO_PD1_CAN0TX);
    
     
    
    // ijungiama CAN periferija
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
    
     
    
    // nustatomas CAN taktavimas
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 16000000);
    
     
    
    //ijungiamas CAN
    CANEnable(CAN0_BASE);
    
     
    
    //kas 5s zinutei priskiriami duomenys ir ji issiunciama
      for (i=0;i<5;i++)
    
      {
    
      zinute.pucMsgData = &duomenys[i];   // rodykle i duomenis
      zinute.ulMsgLen = sizeof(duomenys[i]);  // siunciamu duomenu ilgis
      CANMessageSet(CAN0_BASE, 1, &zinute, MSG_OBJ_TYPE_TX);
      SysCtlDelay(5 * SysCtlClockGet() / 3);  //5s uzlaikymas
    
      } 
    
    }         
    
    
    [code]
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,346
    2,251
    Nov 17, 2011
    You should tell us which microcontroller this is for. It also would be a greta deal more helpful if the comments were in English.

    Many microcontroller manufacturers have code examples for secific tasks (in your case CAN control) available on their websites. Have you had a lok into this?
     
  3. Wantas

    Wantas

    4
    0
    May 30, 2015
    Ouh... ok.. I'm sorry.

    so this is my microcontroller ---> http://www.ti.com/product/lm3s9b90
    and I have to choose "Digital to analog converter" as a device which I have to connect to this microcontroller ...
    and I have to use I2C interface.

    and after this I have to make a code like this...

    Code:
    #include "inc/hw_memmap.h"
    #include "inc/hw_ty pes.h"
    #include "inc/hw_ints.h"
    #include "inc/hw_can.h"
    #include "driverlib/can.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/gpio.h"
    #include "utils/uartstdio.h"
    
    int main(void)
    {
    tCANMsgObject message ;
    int i = 0;
    unsigned char data[30];
    
    message.ulMsgID = 1; // CAN messages ID
    message.ulMsgIDMask = 0; // ID filtration when you are using a mask
    message.ulFlags = 0; // flag
    
    //Synchronisation is determined by an external clock generator
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
    SYSCTL_XTAL_16MHZ);
    
    //activating a general-purpose port D which is necessary for pins
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
    
    //establishing an alternative function for GPIO pins.
    GPIOPinTypeCAN(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
    // pins assigned CAN0 features
    GPIOPinConfigure(GPIO_PD0_CAN0RX);
    GPIOPinConfigure(GPIO_PD1_CAN0TX);
    
    //CAN is switched on periphery
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
    
    // set the CAN Synchronisation
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 16000000);
    
    //activating CAN
    CANEnable(CAN0_BASE);
    
    //assign (attribute) and send data every 5 seconds 
    for (i=0;i<5;i++)
    
    {
    
    message.pucMsgData = &data[i]; //a pointer to the data
    message.ulMsgLen = sizeof(data[i]); //Sending data length
    CANMessageSet(CAN0_BASE, 1, &message, MSG_OBJ_TYPE_TX);
    SysCtlDelay(5 * SysCtlClockGet() / 3); // 5s delay
    
    }
    
    }
    
    
    [code]
    
    so this code is for the task ---> CAN interface/CAN tranceiver ISO1050 [CAN1] (<--connected device)/ Data dispatch every 5 seconds,retrieving values.
    
    and my task is ---> I2C interface/ DAC(Digital to analog converter)/ Recording every 0.5 seconds.
    
    Can anybody make a short code for my task ???? please.
     
  4. Wantas

    Wantas

    4
    0
    May 30, 2015
    So nobody can help me ?? ;?
     
  5. davenn

    davenn Moderator

    13,653
    1,886
    Sep 5, 2009
    That microcontroller is a pretty old one. I'm not familiar with it ... sorry cannot help

    Dave
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-