MSP430F149 microcontroller drives DS2762 to read and write C language program

Publisher:Meilin8888Latest update time:2015-05-15 Source: 51heiKeywords:MSP430F149 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Based on the MSP430F149 microcontroller driving the DS2762 read and write operation C language program, it can respond to keystrokes or other signals, read current and voltage, and perform corresponding processing.
//#include
#define uchar unsigned char
#define uint unsigned int
uint A,B,C,D,E,F,G,H,I,J; //The macro definition C in io430x14x is commented here, please note
#define IO_OUT P3DIR |= BIT0;
#define IO_INP P3DIR &= ~BIT0;
uint data;
/****************************************************** *****************************
Normalize read and write times
Initial debugging uses the parameter standard=1
// Pause for exactly 'tick' number of ticks = 0.25us
The actual measured A delay is 14Us
B125US
*************************************************** ****************************/
void SetSpeed(uint standard)
{
        // Adjust tick values ​​depending on speed
        if (standard)
        {
                // Standard Speed
                A = 6 * 4 / 2;
                B = 64 * 4 / 2;
                C = 60 * 4 / 2;
                D = 10 * 4 / 2;
                E = 9 * 4 / 2;
                F = 55 * 4 / 2;
                G = 0 / 2;
                H = 480 * 4 / 2;
                I = 70 * 4 / 2;
                J = 410 * 4 / 2;
        }
        else
        {
                // Overdrive Speed
                A = 1.5 * 4;
                B = 7.5 * 4;
                C = 7.5 * 4;
                D = 2.5 * 4;
                E = 0.75 * 4;
                F = 7 * 4;
                G = 2.5 * 4;
                H = 70 * 4;
                I = 8.5 * 4;
                J = 40 * 4;
        }
}
/****************************************************** *****************************
Delay Program
Note that you need to use an 8M crystal oscillator with a clock period of 125ns
*************************************************** ****************************/
void tickDelay(uint tick) // Implementation is platform specific
{
  for(;tick>0;tick--);
}
/****************************************************** *****************************
Host reset pulse
When the receiving result is 0, it indicates that the slave responds
*************************************************** ****************************/
uchar OWTouchReset(void)
{
        uchar result;
        IO_OUT;
        tickDelay(G);
        P30 = 0; // Drives DQ low
        tickDelay(H);
        P30 = 1; // Releases the bus
        tickDelay(I);
        IO_INP;
        result = P3IN & 0X01; // Sample for presence pulse from slave
        tickDelay(J); // Complete the reset sequence recovery
        return result; // Return sample presence pulse result
}
/****************************************************** *****************************
Like writing a DS2762 write a bit
Send a 1-Wire write bit. Provide 10us recovery time.
*************************************************** ****************************/
void OWWriteBit(uchar bit)
{
        if (bit)
        {
                // Write '1' bit
                P30 = 0; // Drives DQ low
                tickDelay(A);
                P30 = 1; // Releases the bus
                tickDelay(B); // Complete the time slot and 10us recovery
        }
        else
        {
                // Write '0' bit
                P30 = 0; // Drives DQ low
                tickDelay(C);
                P30 = 1; // Releases the bus
                tickDelay(D);
        }
}[page]
/****************************************************** *****************************
Reading a bit from the DS2762
Read a bit from the 1-Wire bus and return it. Provide 10us recovery time.
*************************************************** ****************************/
uchar OWReadBit(void)
{
        uchar result;
 
        P30 = 0; // Drives DQ low
        tickDelay(A);
        P30 = 1; // Releases the bus
        tickDelay(E);
        result = P3IN & 0X01; // Sample the bit value from the slave
        tickDelay(F); // Complete the time slot and 10us recovery
 
        return result;
}
/****************************************************** *****************************
Like writing a byte to the DS2762
Send a 1-Wire write. Provide 10us recovery time.
The DS2762 feature sends all commands and data with the low-order bit of the byte first, which is the opposite of most serial communication formats.
*************************************************** ****************************/
void OWWriteByte(uchar data)
{
        uchar loop;
 
        // Loop to write each bit in the byte, LS-bit first
        for (loop = 0; loop < 8; loop++)
        {
                OWWriteBit(data & 0x01);
 
                // shift the data byte for the next bit
                data >>= 1;
        }
}
/****************************************************** *****************************
Read a byte from the DS2762
 
*************************************************** ****************************/
uchar OWReadByte(void)
{
        uchar loop, result=0;
 
        for (loop = 0; loop < 8; loop++)
        {
                // shift the result to get it ready for the next bit
                result >>= 1;
 
                // if result is one, then set MS bit
                if (OWReadBit())
                        result |= 0x80;
        }
        return result;
}
/****************************************************** *****************************
Send character function
Operations performed according to the time sequence of the paper diagram
It is not clear that sequential reads need to be optimized through testing.
Solution 1: Issue a read command and read one address twice. This should be correct.
Solution 2: Issue a read command and send two addresses to read twice
*************************************************** ****************************/
uint readvoltage(void)
{
         uchar j = 1;
         unsigned char volhigh,vollow;
         while(j) //Check if 2762 responds
         {
            j = OWTouchReset();
         }
OWWriteByte(0xcc); //
         OWWriteByte(0x69);
         OWWriteByte(0x0c);
         vollow = OWReadByte();
         volhigh = OWReadByte();
         return ((volhigh<<8)|(vollow)); //Merge two bytes
}
/****************************************************** *****************************
Clock initialization
Note that you need to use an 8M crystal oscillator with a clock period of 125ns
*************************************************** ****************************/
void Init_clk(void)
{unsigned char i; 
 //Clock settings for the time base module 
 //When the MCU is powered on, the source of the MCLK master clock is selected by default as provided by DCO. The default DCO of the F1 series is 800KHZ. 
 //ACLK auxiliary clock defaults to XT1, XT1 is usually connected to 32768HZ crystal. 
 //The default SMCLK sub-clock is DCO, which is also 800KHZ. 
 //XT2 needs to be manually turned on and tested to see if it is turned on successfully. 
 BCSCTL1 &= ~(XT2OFF + XTS); //Start XT2 high-speed clock module 
 BCSCTL2 |= SELM1; //MCLK master clock selects XT2 as the clock source. TX2 input is not divided. 
 BCSCTL2 &= ~SELS; //SMCLK is selected as DCO as the clock source. (reference) 
  
 //Just turned on XT2, it takes a while for XT2 to enter a stable state. So you need to wait and detect the stable state. 
 //Usually use do...for syntax, which is the program writing method recommended by TI 
 do 
 { 
  IFG1 &=~OFIFG; //Clear OSCFault flag 
  for(i=0xff;i>0;i--) //Delay and wait for it to start and stabilize 
    ; 
 } 
 while((IFG1 & OFIFG) !=0); //Check if the OSCFault flag is 0. If it is 0, it means XT2 is stable. 
                            //Otherwise keep waiting... 
}
/****************************************************** *****************************
Main function
Note that you need to use an 8M crystal oscillator with a clock period of 125ns
*************************************************** ****************************/
void main()
{
  WDTCTL = WDTPW | WDTHOLD; // Stop watchdog
  Init_clk();
  SetSpeed(1);
  while(1)
  {
    data = readvoltage();
  }
 
}
Keywords:MSP430F149 Reference address:MSP430F149 microcontroller drives DS2762 to read and write C language program

Previous article:Chapter 2 Some Data Units (X86 Assembly Tutorial)
Next article:Backtracking algorithm 1

Recommended ReadingLatest update time:2024-11-16 16:47

LCD1602 low-level driver subroutine
Crystal oscillator 11.0592M /**********************LCD1602 bottom driver program***************************/ /********************************************************************/ //delay: delay xms void delay(uint xms) { uchar i, j; for(i = 0; i xms; i++)   for(j = 0; j 110; j++); } //wait: busy waiting v
[Microcontroller]
[MCU Notes] OLED controller SSD1306 and driver code
Preface: I have rarely used OLED for a long time, and even if I did, I would refer to the code of experts. Recently, I had to study it again carefully. Write it down so that I don't forget it. The 0.61-inch OLED I have on hand has a resolution of 96*16, which means there are 96 dots horizontally and 16 dots vertic
[Microcontroller]
[MCU Notes] OLED controller SSD1306 and driver code
Using mixed signal high voltage microcontroller to realize LED buck-boost drive circuit
LED Background Knowledge In recent years, LEDs have become a viable new light source that is no longer just used as a "status indicator" for electronic devices. Technological advances have made LEDs typically more than three times more efficient than incandescent bulbs, and they are also very durable, with a li
[Microcontroller]
Using mixed signal high voltage microcontroller to realize LED buck-boost drive circuit
51 MCU IO port driving capability, sink current, pull current, and pull-up resistor selection
The pins of a microcontroller can be controlled by a program to output high or low levels, which can be considered the output voltage of the microcontroller. However, the program cannot control the output current of the microcontroller. The output current of the microcontroller depends largely on the external devices o
[Microcontroller]
51 MCU IO port driving capability, sink current, pull current, and pull-up resistor selection
51 MCU drives I2C (24C02) assembly language program
;Program description: Write the contents of 55h~58h inside the MPU to 24c02 via the I2C bus from external interrupt 0, and then read the written contents to  ;MPU internal data memory 60H~~63H    SCL EQU P1.1   SDA EQU P1.0   org 0000h  jmp begin  org 0003H  ljmp write  org 0013H  ljmp read   
[Microcontroller]
51 MCU driver for proximity switch
Today I made a little thing: the digital tube displays the change value of the proximity switch. The proximity switch is connected to the microcontroller through external interrupt 0; the digital tube reading increases to 50 and then clears to zero; #include reg51.h // Reference the header file of the standard libra
[Microcontroller]
Design of driving circuit for multi-channel PZT based on single chip microcomputer
Abstract: A driving circuit for controlling multiple PZTs (piezoelectric ceramics) based on C8051F005 single-chip microcomputer is designed. The serial data transmission method is adopted, and the characteristics of the new digital-to-analog converter AD5308 with 8-channel DAC output are utilized to greatly simplify
[Industrial Control]
Design of driving circuit for multi-channel PZT based on single chip microcomputer
PIC microcontroller drives LED digital tube display program
;*****This program is used to drive the LED digital tube display, and display the numbers 1, 2, 3, 4, 5, 6, 7, 8 on the 8 LED digital tubes in sequence******* ;****http://www.51hei.com The classic program of the microcontroller learning network has been tested. If the common cathode and common anode of the LED
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号