搜索
bottom↓
12
返回列表 发新帖
楼主: wenziqi

SmartM51&AVR电子书籍教材-《划时代-51单片机C语言全新教程》和所有源代码

[复制链接]

出0入0汤圆

发表于 2010-10-2 22:44:24 | 显示全部楼层
好东西,谢谢.

出0入0汤圆

发表于 2010-10-25 14:47:16 | 显示全部楼层
好东西 看看

出5入8汤圆

发表于 2010-10-25 15:46:00 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-10-26 12:51:14 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-10-26 16:18:20 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-10-27 13:57:42 | 显示全部楼层
好东西 看看

出0入0汤圆

发表于 2010-11-8 14:18:25 | 显示全部楼层
回复【楼主位】wenziqi
-----------------------------------------------------------------------

有没有出书咯。温大哥,想买本呢。很好

出0入0汤圆

发表于 2010-11-8 23:10:01 | 显示全部楼层
不错下来看看

出0入0汤圆

发表于 2010-11-9 21:09:26 | 显示全部楼层
谢谢,收下有空再看

出0入0汤圆

发表于 2010-11-11 11:36:59 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-11-24 23:20:33 | 显示全部楼层
收藏

出0入0汤圆

发表于 2010-11-24 23:58:03 | 显示全部楼层
you can significantly improve many of the examples in the book.

for example, here is your hc164 code:

======original code===========
/***************************************************
* 大量宏定义,便于代码移植和阅读
***************************************************/
#define HIGH 1
#define LOW 0
#define LS164_DATA(x) {if((x))P0_4=1;else P0_4=0;}
#define LS164_CLK(x) {if((x))P0_5=1;else P0_5=0;}
#define SEG_PORT P0 //控制数码管字型码端口

/****************************************
*函数名称:LS164Send
*输入:无
*输出:无
*功能:74LS164 发送单个字节
******************************************/
void LS164Send(unsigned char byte)
{
unsigned char j;
for(j=0;j<=7;j++)//对输入数据进行移位检测
{
if(byte&(1<<(7-j))) //检测字节当前位
{
LS164_DATA(HIGH); //串行数据输入引脚为高电平
}
else
{
LS164_DATA(LOW); //串行数据输入引脚为低电平
}
LS164_CLK(LOW); //同步时钟输入端以一个上升沿结束确定该位的值
LS164_CLK(HIGH);
}
}
/*******
======end code========

a few issues with it:

1) the code isn't very portable: you have hard-coded DATA on P0.4 and SCK on P0.5. to the extent that you want to change it, you have to recode the macros and then their initialization portion - to make them into the output mode (should you port the code to a chip with DDR).
2) the hc164 portion of the code isn't efficient.

a better approach is to define the port the data/sck pins are on and then write to those pins LOGICALLY.

here is an example:

========alternative header=============
#define SPI_PORT                        P2
#define SPI_PORT_IN                        P2
#define SPI_DDR                                P2
//#define SPI_CS                                (1<<0)
#define SPI_SCK                                (1<<0)                                        //serial clock
#define SPI_MOSI                        (1<<3)                                        //mask out slave in
#define SPI_MISO                        (1<<1)                                        //master in slave out
#define SPI_DLY                                0
#define SPI_DELAY(n)                delay(n)                                //some delay
#define SPI_SELECT(cs)                {IO_CLR(SPI_PORT, cs); SPI_DELAY(SPI_DLY); IO_OUT(SPI_DDR, cs);}        //select the chip
#define SPI_DESELECT(cs)        {IO_SET(SPI_PORT, cs); SPI_DELAY(SPI_DLY); IO_OUT(SPI_DDR, cs);}        //deselect the chip
#define spi_select(cs)                SPI_SELECT(cs)                        //select the spi attached to cs pin
#define spi_deselect(cs)        SPI_DESELECT(cs)                //deselect the spi attached to cs pin

void spi_init(void);                        //initialize the spi

void spi_send(unsigned char data_t);                //send data_t over spi

unsigned char spi_read(void);                                //read data from spi
=========end header================

========alternative source========
void spi_init(void) {
        IO_IN(SPI_DDR, SPI_MISO);                //input pins

        IO_CLR(SPI_PORT, SPI_SCK | SPI_MOSI);        //clear output pins
        //IO_SET(SPI_PORT, cs);                //unselect the chip
        IO_OUT(SPI_DDR, SPI_SCK | SPI_MOSI);        //set pins as output
}

//data strobe out on the rising edge of sck
void spi_send(unsigned char data_t) {
        unsigned char mask = 0x80;                //start with the most signifcant bit

        //IO_CLR(SPI_PORT, SPI_SCK);                //default state of sck: low
        while (mask) {
                IO_CLR(SPI_PORT, SPI_SCK);        //clear spi_sck
                if (data_t & mask) IO_SET(SPI_PORT, SPI_MOSI);        //send the data
                else IO_CLR(SPI_PORT, SPI_MOSI);
                IO_SET(SPI_PORT, SPI_SCK);        //send on the rising edge
                //SPI_DELAY(SPI_DLY);                        //insert some delays to generate 50% dc
                mask = mask >> 1;                        //next bit
        }
        //IO_CLR(SPI_PORT, SPI_MOSI);                //clear mosi
        IO_CLR(SPI_PORT, SPI_SCK);
}
======end alternative==========

so if you were to change the pin assignment, you just need to redefine _SCK, _MOSI or _MISO and the code will work.

or if you wish to send multi-bytes or odd bits over spi, you can modify the mask variable in the spi_send() routine to achieve that.

many (most) of  your code pieces in the book can benefit from this approach.

出0入0汤圆

发表于 2010-11-24 23:59:59 | 显示全部楼层
as a follow-up, the following is an example of using the above code to send either 16-bit or 32-bit data over spi.

==================code==================
//sending multi-byte data using 8-bit spi routines
#include <regx51.h>
#include "gpio.h"
#include "delay.h"
#include "spi_sw.h"

#define SPI_CS0                        (1<<7)                                        //cs0 on p.7

typedef union {
        unsigned long ul;                                                        //32bit data type
        unsigned char uc[4];                                                //most significant byte
} spi32_t;

typedef union {
        unsigned short ui;                                                        //32bit data type
        unsigned char uc[2];                                                //most significant byte
} spi16_t;

void spi32_send(unsigned long ul) {
        spi32_t tmp;
       
        tmp.ul=ul;                                                                        //assign 32-bit value to tmp;
        //msb/lsb orientation is compiler dependent
        spi_send(tmp.uc[0]);                                                //send the msb
        spi_send(tmp.uc[1]);
        spi_send(tmp.uc[2]);
        spi_send(tmp.uc[3]);                                                //send the lsb
}

void spi16_send(unsigned short ui) {
        spi16_t tmp;
       
        tmp.ui=ui;                                                                        //assign 16-bit value to tmp;
        //msb/lsb orientation is compiler dependent
        spi_send(tmp.uc[0]);                                                //send the msb
        spi_send(tmp.uc[1]);                                                //send the lsb
}

void mcu_init(void) {
}

int main(void) {
        mcu_init();                                                                        //reset the mcu
        spi_init();                                                                        //reset the mcu
        spi_select(SPI_CS0);                                                //select cs0
        //spi32_send(0xf05522aa);                                                //send 32-bit data
        spi16_send(0x22aa);                                                //send 16-bit data
        spi_deselect(SPI_CS0);                                                //deselect cs0
        while (1) {
        }
}
=========end code==============

the same code will work on pretty much any mcu.

出0入0汤圆

发表于 2010-11-25 00:27:37 | 显示全部楼层
here is a test run, using my code to send 0x22 and your code to send 0x22 to a hc164.

======code===========
//sending multi-byte data using 8-bit spi routines
#include <regx51.h>
#include "gpio.h"
#include "delay.h"
#include "spi_sw.h"

#define SPI_CS0                        (1<<7)                                        //cs0 on p.7

/***************************************************
* ?????,?????????
***************************************************/
#define HIGH 1
#define LOW 0
#define LS164_DATA(x) {if((x))P2_3=1;else P2_3=0;}
#define LS164_CLK(x) {if((x))P2_0=1;else P2_0=0;}
//#define SEG_PORT P0 //??????????

/****************************************
*????:LS164Send
*??:?
*??:?
*??:74LS164 ??????
******************************************/
void spi_send2(unsigned char byte)
{
        unsigned char j;
        for(j=0;j<=7;j++)//???????????
        {
                if(byte&(1<<(7-j))) //???????
                {
                        LS164_DATA(HIGH); //????????????
                }
                else
                {
                        LS164_DATA(LOW); //????????????
                }
                LS164_CLK(LOW); //?????????????????????
                LS164_CLK(HIGH);
        }
}

void mcu_init(void) {
        IO_SET(SPI_PORT, SPI_CS0);
        IO_OUT(SPI_DDR, SPI_CS0);
}

int main(void) {
        mcu_init();                                                                        //reset the mcu
        spi_init();                                                                        //reset the mcu
        IO_FLP(SPI_PORT, SPI_CS0);                                        //start of the 1st spi send routien
        //spi32_send(0xf05522aa);                                                //send 32-bit data
        spi_send(0x22);                                                //send 32-bit data
        IO_FLP(SPI_PORT, SPI_CS0);                                        //start of the 1st spi send routien
        spi_send2(0x22);
        IO_FLP(SPI_PORT, SPI_CS0);                                        //start of the 1st spi send routien
        while (1) {
        }
}
========end==========

here is the waveform.


(原文件名:31. spi comp.PNG)



it took my code about 180us to send a byte, and 420us for yours to send the same byte.

and the your sck waveform has severe dc content as its DC % is close to 100% - it should be close to 50%.

出0入0汤圆

发表于 2010-11-25 00:29:29 | 显示全部楼层
if you trace the wave form, you will find that your code spends way too much time on the "if" statement, because it has to do a subtraction, shifting it, do an AND operation, and then decide if it is high or not.

my code is much more efficient on that front.

出0入0汤圆

发表于 2010-11-25 08:13:31 | 显示全部楼层
目录一看很NB,但是每章的内柔太少了,不是很细 啊!有的还不如不写……

出0入0汤圆

发表于 2010-11-25 09:09:50 | 显示全部楼层
mark

出0入0汤圆

发表于 2010-11-25 09:12:15 | 显示全部楼层
先下下来,呵呵,mark

出0入0汤圆

发表于 2010-11-25 09:38:33 | 显示全部楼层
看看!!

出0入0汤圆

发表于 2010-11-29 22:45:42 | 显示全部楼层
这论坛确实牛啊。

出0入0汤圆

发表于 2011-2-26 09:46:37 | 显示全部楼层
貌似不错,看看

出0入0汤圆

发表于 2011-3-4 22:50:27 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-7 09:22:41 | 显示全部楼层
下来先看看。谢谢

出0入0汤圆

发表于 2011-3-7 10:39:20 | 显示全部楼层
http://www.ourdev.cn/bbs/bbs_content.jsp?bbs_sn=4094929&bbs_page_no=1&bbs_id=9999#

这有个类似的帖子,,

出0入0汤圆

发表于 2011-3-9 01:34:06 | 显示全部楼层
看样子不错,下载看看

出0入0汤圆

发表于 2011-3-9 08:43:22 | 显示全部楼层
谢谢楼主分享。

出0入0汤圆

发表于 2011-3-9 15:42:33 | 显示全部楼层
mark

出0入0汤圆

发表于 2011-3-12 19:39:06 | 显示全部楼层
ha ha
谢谢楼主啦
一个时代就这样呗楼主划过~~~~

出0入0汤圆

发表于 2016-10-28 00:50:23 | 显示全部楼层
看看              

出0入0汤圆

发表于 2016-11-28 12:26:38 | 显示全部楼层
下载学习  

出0入0汤圆

发表于 2016-11-30 23:06:41 | 显示全部楼层
谢谢分享,下了收藏。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 16:10

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表