微软公司宣布不再支持你正在使用的 IE浏览器,这会严重影响浏览网页,请使用微软最新的Edge浏览器
您好, 登录| 注册|

上传一份完整的SPWM code in C,旨在提高国人的设计水平

  • 2006-04-24 23:39
  • zack

    LV.0
  • 5.1w

    浏览

  • 259

    回复

  • 4

    获赞

  • /*****************************************************************************
    * Filename: MAIN.C
    ******************************************************************************
    *
    * Author: Dave Karipides
    * Company: APS, Inc.
    * Date: 3-3-97
    * Compiled Using MPLAB-C Rev 1.21
    *
    ******************************************************************************

    ******************************************************************************
    *
    * Description: The main routine calls all the functions for generating
    * an OPEN_LOOP or FEEDBACK sine wave of either 50 or 60 Hz.
    *
    ******************************************************************************
    *

    *
    *
    ******************************************************************************/

    /*****************************************************************************
    * main()
    *
    * Description: The main routine initializes the registers and loops
    * forever.  All control is handled in the TMR0 INT
    * routine.
    *
    *
    * Input Variables: NONE
    *
    * Output Variables: NONE
    *
    *
    ******************************************************************************/

    //#define OPEN_LOOP
    #define FEEDBACK
    //#define 50Hz
    #define 60Hz

    #pragma option v
    #include <17c43.h>
    #include
    #include

    #ifdef OPEN_LOOP
    // This table yields Full VRMS input
    unsigned char const pwmtab[32]={0,25,50,74,98,120,142,162,180,197,212,
    225,235,244,250,254,255,254,250,244,235,
    225,212,197,180,162,142,120,98,74,50,25};
    #endif
    #ifdef FEEDBACK
    // This table yields slightly less than Full VRMS input
    unsigned char const pwmtab[32]={0,20,40,60,79,97,114,131,145,159,171,
    181,189,197,202,205,206,205,202,197,189,
    181,171,159,145,131,114,97,79,60,40,20};
    #endif

    long read_ad(unsigned char); // Prototype for A/D converter function

    unsigned char index; // Index into the sinewave reference table
    unsigned char sign; // Flag used to unfold sinewave reference table
    long reference; // Value of the sinewave refrence after unfolding
    unsigned char reference_lo  @ reference; // V1.21 of Compiler does not type cast unsigned
    // char to long so we will write to low byte separately
    long out_volt; // Magnitude of the output voltage;
    long y; // Variables used in compensation routine
    long yold;
    long x;
    long xold;
    long ad_value; // A/D Converter Value

    void main(void)
    {
    CLRWDT();
    PORTC = 0; // Zero out portc latches
    DDRC = 0x22; // Set up Data direction register for C
    DDRB = 0; // Set up Data direction register for B
    PR1 = 0xFF; // Setup PR1 register (24.4Khz @ 25Mhz clk)
    PW1DCL = 0; // Set low bits of PWM to 0
    PW1DCH = 0; // Init PWM duty cycle to 0

    T0STA = 0x20; // Configure Timer0 prescaler
    INTSTA.T0IE = 1; // Enable Timer 0 interrupt
    TCON1.TMR1CS = 0;
    TCON1.T16 = 0;
    TCON2.TMR1ON = 1; // Start timer 1 (PWM timer)
    TCON2.PWM1ON = 1; // Turn on the PWM
    CPUSTA.GLINTD = 0; // Unmask the interrupts

    index = 0; // Initialize variables
    sign = 0;
    y = 0;
    yold = 0;
    x = 0;
    xold = 0;
    PORTC.0 = 1; // Enable the Inverter
    while(1); // Loop forever, execute in INT Routine
    }

    #ifdef FEEDBACK
    __TMR0() // Timer  interrupt
    {
    T0STA.T0CS = 0; // Stop timer
    PORTB.7=1;
    #ifdef 60Hz
    TMR0L=0xA5;
    TMR0H=0xF9; // Make Timer0 interrupt at 3.84KHz for 60Hz output
    #endif
    #ifdef 50Hz
    TMR0L=0x5F; // Make Timer0 interrupt at 3.20KHz for 50Hz output
    TMR0H=0xF8;
    #endif
    T0STA.T0CS = 1; // Start timer
    CLRWDT();

    reference = 0; // Clear Reference Value
    reference_lo = pwmtab[index]; // Lookup the value of the sinewave reference

    if (!index) // Toggle Sign Every Cycle Through table
    sign = ~sign;
    ++index; // Increment index
    if (index == 32) // If end of table, reset counter
    index = 0;
    if (sign) // If negative going wave
    {
    reference = ~reference; // V1.21 of Compiler negate (-ref) doesn't work for
    reference = reference + 1;// ref<=0
    }
    ad_value = read_ad(0);
    out_volt = ad_value - 512; // Read output voltage (512 counts=0 volts out)

    // Form the expression y = yold + (0.09261 * (x + xold))
    // Where yold, xold is the value of y, x from the previous sample
    // x is the , formed by the difference between the output
    // of the inverter and the reference signal.
    x = out_volt - reference;
    y = ((x + xold) * 24);
    y = y / 256;
    y = y + yold;
    if (y >= 0)
    {
    PORTC.2 = 0; // Set positive going cycle
    } else
    {
    PORTC.2 = 1; // Set negative going cycle
    y = ~y;
    y = y + 1;
    }
    if (y > 255)
    y = 255; // Limit y
    PW1DCH = y; // Update duty cycle
    xold = x; // Store previous sample's state
    yold = y;
    PORTB.7=0;
    }
    #endif

    #ifdef OPEN_LOOP
    // The inverter runs in an open loop mode with OPEN_LOOP defined.
    __TMR0() // Timer interrupt
    {
    T0STA.T0CS = 0; // Stop timer
    #ifdef 60Hz
    TMR0L=0xA5;
    TMR0H=0xF9; //Make Timer0 interrupt at 3.84KHz for 60Hz output
    #endif
    #ifdef 50Hz
    TMR0L=0x5F; //Make Timer0 interrupt at 3.20KHz for 50Hz output
    TMR0H=0xF8;
    #endif
    T0STA.T0CS=1; //Start timer
    CLRWDT();

    PW1DCH = pwmtab[index];
    if (!index)
    {
    PORTC.0 = 0; // Gate Drive off
    PORTC.2 = ~PORTC.2; // Flip Pos/Neg bit
    PORTC.0 = 1; // Gate Drive on
    }
    ++index;
    if (index == 32)
    index = 0;
    PORTC.3 = ~PORTC.3; // Toggle bit to test freq.
    }
    #endif

    long read_ad(unsigned char channel)
    {
    long result;

    PORTC.6 = 1; // Write bit high
    PORTC.7 = 1; // Read bit high
    PORTC.4 = 1; // Chip select high
    DDRD = 0; // Make PORTD an output
    PORTD = 0x04; // Single ended mode signed 10 bit chan 0 Right justified
    PORTC.4 = 0; // Select chip
    PORTC.6 = 0; // latch command word int A/D
    PORTC.6 = 1; // Start conversion
    PORTC.4 = 1; // Deselect chip
    while (PORTC.5); // Wait for conversion to complete
    DDRD = 0xFF; // Make PORTD an input
    PORTC.4 = 0; // Select chip
    PORTC.7 = 0; // Read high byte
    *( ((unsigned char*)&result) + 1) = PORTD;
    PORTC.7 = 1;
    PORTC.4 = 1;
    PORTC.4 = 0;
    PORTC.7 = 0; // Read low byte
    *( ((unsigned char*)&result) ) = PORTD;
    PORTC.7 = 1;
    PORTC.4 = 1; // Reset chip select lines
    return (result); // Return data
    }

    同是电子工程师,请一定不要吝啬你的赞!

    4人已赞

    编辑 举报

    LV.1

    647433

    84658

    13

    40850

    说说你的看法

  • LV.

    @

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    取消
    发送
  • 现在还没有回复呢,说说你的想法

    现在还没有回复呢,说说你的想法

    全部回复(259)

  • zack

    LV.1

    2006-04-24 23:53

    @

    转帖海魄兄的话:
    各位朋友,我以前在交友\同僚转帖过一个网友讲的故事,希望在这里再次提起,希望能给我们启发,在以前的北美洲,送牛奶的把灌满牛奶的奶瓶放到到每家订牛奶的客户家的收牛奶的地方以后,经常会有两种鸟来偷食,其中一种是红嘴鸟,一种是麻雀.后来送牛奶的人就想了个办法,把灌满牛奶的瓶口用锡箔包上,这样就不会被鸟偷食了.可是过了几年以后,麻雀就用嘴把锡箔弄破了继续偷吃牛奶.而红嘴鸟至今还无法偷吃到牛奶.为什么麻雀可以而红嘴鸟不可以呢?有关研究人员对这个现象进行了研究,结论是,因为麻雀是群居,偶尔有一只发现了可以有新的办法吃到牛奶就会告诉其它麻雀,而红嘴鸟却不是,每个个体都是单独行动.我希望我们在国内从事UPS的技术人员能够从这个小的故事得到启发,来到这里的每位朋友不论资历高底,希望大家能真正的从技术角度出发,共同交流共同学习,共同促进,那样才能缩短我们和国外的技术差距.


    这份源代码希望大家提出看法,我的设计基本上源自此处.
    I hope the inverter field R&Ds in our country can get help from this code  and become more and more experience and skills in MCU software design.
    I hope our engineers would have more and more creativity,not just except copy there is olny copy!
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647434

    84658

    13

    40850

    取消
    发送
    2
  • zack

    LV.1

    2006-04-25 00:12

    @

    想必有人知道这份代码的出处可是为什么在论坛这么久,就看不到有人帖出呢?好晕啊!
    而,对于那些只会叫给代码一份,图纸一份的朋友,你们有没有真正地做到努力去提高自己的技术水平呢?
    而,有写真正有水平的老师们,一些技术没什么好保留的,在国外那些都已经很落后了.
    1

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647439

    84658

    13

    40850

    取消
    发送
    3
  • hwhpcb

    LV.1

    2006-04-25 07:51

    @zack

    想必有人知道这份代码的出处可是为什么在论坛这么久,就看不到有人帖出呢?好晕啊!
    而,对于那些只会叫给代码一份,图纸一份的朋友,你们有没有真正地做到努力去提高自己的技术水平呢?
    而,有写真正有水平的老师们,一些技术没什么好保留的,在国外那些都已经很落后了.
    顶!
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647440

    84658

    13

    50010

    取消
    发送
    4
  • xzszrs

    LV.1

    2006-04-25 09:22

    @zack

    想必有人知道这份代码的出处可是为什么在论坛这么久,就看不到有人帖出呢?好晕啊!
    而,对于那些只会叫给代码一份,图纸一份的朋友,你们有没有真正地做到努力去提高自己的技术水平呢?
    而,有写真正有水平的老师们,一些技术没什么好保留的,在国外那些都已经很落后了.
    严重同意!顶!UP!
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647441

    84658

    13

    70075

    取消
    发送
    5
  • yujunhuihao

    LV.1

    2006-04-25 11:08

    @zack

    想必有人知道这份代码的出处可是为什么在论坛这么久,就看不到有人帖出呢?好晕啊!
    而,对于那些只会叫给代码一份,图纸一份的朋友,你们有没有真正地做到努力去提高自己的技术水平呢?
    而,有写真正有水平的老师们,一些技术没什么好保留的,在国外那些都已经很落后了.
    看到这些,我想起一个不沾边的事实,中国的足球为什么老踢不好,恐怕就是缺乏团队意识,我们搞技术的国人似乎也是如此,宁愿把东西窝霉也不愿意拿出来,我觉得可以建立一个真正大家把自己的资源共享出来以提高大家水平的板块.
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647442

    84658

    13

    85493

    取消
    发送
    6
  • zack

    LV.1

    2006-04-25 11:31

    @

    这是一份查表法输出SPWM的程序!是完整的,希望对大家有帮助!
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647447

    84658

    13

    40850

    取消
    发送
    7
  • lcwell

    LV.1

    2006-04-29 10:20

    @zack

    这是一份查表法输出SPWM的程序!是完整的,希望对大家有帮助!
    ding  国人要自强啊,团结就是力量
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647448

    84658

    13

    83507

    取消
    发送
    8
  • liyuanlinups

    LV.1

    2006-05-02 01:56

    @zack

    转帖海魄兄的话:
    各位朋友,我以前在交友\同僚转帖过一个网友讲的故事,希望在这里再次提起,希望能给我们启发,在以前的北美洲,送牛奶的把灌满牛奶的奶瓶放到到每家订牛奶的客户家的收牛奶的地方以后,经常会有两种鸟来偷食,其中一种是红嘴鸟,一种是麻雀.后来送牛奶的人就想了个办法,把灌满牛奶的瓶口用锡箔包上,这样就不会被鸟偷食了.可是过了几年以后,麻雀就用嘴把锡箔弄破了继续偷吃牛奶.而红嘴鸟至今还无法偷吃到牛奶.为什么麻雀可以而红嘴鸟不可以呢?有关研究人员对这个现象进行了研究,结论是,因为麻雀是群居,偶尔有一只发现了可以有新的办法吃到牛奶就会告诉其它麻雀,而红嘴鸟却不是,每个个体都是单独行动.我希望我们在国内从事UPS的技术人员能够从这个小的故事得到启发,来到这里的每位朋友不论资历高底,希望大家能真正的从技术角度出发,共同交流共同学习,共同促进,那样才能缩短我们和国外的技术差距.


    这份源代码希望大家提出看法,我的设计基本上源自此处.
    I hope the inverter field R&Ds in our country can get help from this code  and become more and more experience and skills in MCU software design.
    I hope our engineers would have more and more creativity,not just except copy there is olny copy!
    怎么我一点也看不懂的.指教一下好吗?
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647435

    84658

    13

    92849

    取消
    发送
    9
  • zack

    LV.1

    2006-05-03 18:44

    @liyuanlinups

    怎么我一点也看不懂的.指教一下好吗?
    你有什么地方不明白的,请提出问题...
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647436

    84658

    13

    40850

    取消
    发送
    10
  • zack

    LV.1

    2006-05-03 18:54

    @

    纵观整个论谈,我觉得这份资料是最有价值的,最有实际意义的.
    版主加精哦.
    1146653668.pdf
    0

    设为最佳答案

    置顶

    编辑

    删除

    举报

    #该内容仅管理员可见#

    #回复内容已被删除#

    #该内容正在审核#

    回复:

    647449

    84658

    13

    40850

    取消
    发送
    11
  • 现在还没有回复呢,说说你的想法

    1 2 3 4 5 6  
  • 回复

  • 收藏

  • 点赞

  • 举报有害信息

  • 已超出发布时间24小时,无法编辑与删除
    关于我们 联系方法 广告服务 会议服务 电子星球APP 网站地图 不良信息举报 热线:400-003-2006
    © 2002-2023 Netbroad(网博互动)公司版权所有 津ICP备 11006234号-2 联网备案号:12010402000747 增值电信业务经营许可证:津B2-20120058